* dir.c (glob_helper): get rid of possible memory leak.

* win32/win32.c (cmdglob, rb_w32_cmdvector, rb_w32_opendir,
  rb_w32_get_environ): not to use GC before initialization.
  [ruby-core:09024]


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11245 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2006-10-30 14:23:46 +00:00
parent 3679754a95
commit 66d43b5fd6
3 changed files with 108 additions and 30 deletions

View file

@ -410,7 +410,8 @@ init_env(void)
NTLoginName = "<Unknown>";
return;
}
NTLoginName = ALLOC_N(char, len+1);
NTLoginName = (char *)malloc(len+1);
if (!NTLoginName) return;
strncpy(NTLoginName, env, len);
NTLoginName[len] = '\0';
}
@ -1059,10 +1060,12 @@ insert(const char *path, VALUE vinfo)
NtCmdLineElement *tmpcurr;
NtCmdLineElement ***tail = (NtCmdLineElement ***)vinfo;
tmpcurr = ALLOC(NtCmdLineElement);
tmpcurr = (NtCmdLineElement *)malloc(sizeof(NtCmdLineElement));
if (!tmpcurr) return -1;
MEMZERO(tmpcurr, NtCmdLineElement, 1);
tmpcurr->len = strlen(path);
tmpcurr->str = ALLOC_N(char, tmpcurr->len + 1);
tmpcurr->str = (char *)malloc(tmpcurr->len + 1);
if (!tmpcurr->str) return -1;
tmpcurr->flags |= NTMALLOC;
strcpy(tmpcurr->str, path);
**tail = tmpcurr;
@ -1084,20 +1087,21 @@ cmdglob(NtCmdLineElement *patt, NtCmdLineElement **tail)
char buffer[MAXPATHLEN], *buf = buffer;
char *p;
NtCmdLineElement **last = tail;
int status;
if (patt->len >= MAXPATHLEN)
buf = ruby_xmalloc(patt->len + 1);
if (!(buf = malloc(patt->len + 1))) return 0;
strncpy (buf, patt->str, patt->len);
buf[patt->len] = '\0';
for (p = buf; *p; p = CharNext(p))
if (*p == '\\')
*p = '/';
ruby_brace_glob(buf, 0, insert, (VALUE)&tail);
status = ruby_brace_glob(buf, 0, insert, (VALUE)&tail);
if (buf != buffer)
free(buf);
if (last == tail) return 0;
if (status || last == tail) return 0;
if (patt->flags & NTMALLOC)
free(patt->str);
free(patt);
@ -1321,8 +1325,8 @@ rb_w32_cmdvector(const char *cmd, char ***vec)
}
}
curr = ALLOC(NtCmdLineElement);
MEMZERO(curr, NtCmdLineElement, 1);
curr = (NtCmdLineElement *)calloc(sizeof(NtCmdLineElement), 1);
if (!curr) goto do_nothing;
curr->str = base;
curr->len = len;
@ -1347,7 +1351,18 @@ rb_w32_cmdvector(const char *cmd, char ***vec)
}
len = (elements+1)*sizeof(char *) + strsz;
buffer = ALLOC_N(char, len);
buffer = (char *)malloc(len);
if (!buffer) {
do_nothing:
while (curr = cmdhead) {
cmdhead = curr->next;
if (curr->flags & NTMALLOC) free(curr->str);
free(curr);
}
free(cmdline);
for (vptr = *vec; *vptr; ++vptr);
return vptr - *vec;
}
//
// make vptr point to the start of the buffer
@ -1447,6 +1462,7 @@ rb_w32_opendir(const char *filename)
fh = FindFirstFile(scanname, &fd);
if (fh == INVALID_HANDLE_VALUE) {
errno = map_errno(GetLastError());
free(p);
return NULL;
}
@ -1456,9 +1472,14 @@ rb_w32_opendir(const char *filename)
//
idx = strlen(fd.cFileName)+1;
p->start = ALLOC_N(char, idx);
if (!(p->start = (char *)malloc(idx)) || !(p->bits = (char *)malloc(1))) {
error:
rb_w32_closedir(p);
FindClose(fh);
errno = ENOMEM;
return NULL;
}
strcpy(p->start, fd.cFileName);
p->bits = ALLOC_N(char, 1);
p->bits[0] = 0;
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
SetBit(p->bits, 0);
@ -1484,14 +1505,14 @@ rb_w32_opendir(const char *filename)
Renew (p->start, idx+len+1, char);
if (p->start == NULL) {
rb_fatal ("opendir: malloc failed!\n");
goto error;
}
strcpy(&p->start[idx], fd.cFileName);
if (p->nfiles % 4 == 0) {
Renew (p->bits, p->nfiles / 4 + 1, char);
if (p->bits == NULL) {
rb_fatal ("opendir: malloc failed!\n");
goto error;
}
p->bits[p->nfiles / 4] = 0;
}
@ -3981,10 +4002,12 @@ rb_w32_get_environ(void)
for (env = envtop, num = 0; *env; env += strlen(env) + 1)
if (*env != '=') num++;
myenvtop = ALLOC_N(char*, num + 1);
myenvtop = (char **)malloc(sizeof(char *) * (num + 1));
for (env = envtop, myenv = myenvtop; *env; env += strlen(env) + 1) {
if (*env != '=') {
*myenv = ALLOC_N(char, strlen(env) + 1);
if (!(*myenv = (char *)malloc(strlen(env) + 1))) {
break;
}
strcpy(*myenv, env);
myenv++;
}