4
0
mirror of git://sourceware.org/git/newlib-cygwin.git synced 2025-01-18 12:29:32 +08:00

* libc/posix/wordexp.c (wordexp): Handle expanded words longer

than 500 bytes.
This commit is contained in:
Corinna Vinschen 2012-10-09 09:20:46 +00:00
parent ca8170a6c3
commit 4a6ec9ec52
2 changed files with 25 additions and 7 deletions

View File

@ -1,3 +1,8 @@
2012-10-09 Peter Rosin <peda@lysator.liu.se>
* libc/posix/wordexp.c (wordexp): Handle expanded words longer
than 500 bytes.
2012-10-09 Peter Rosin <peda@lysator.liu.se> 2012-10-09 Peter Rosin <peda@lysator.liu.se>
* libc/posix/wordexp.c (wordexp): Don't leak file streams. * libc/posix/wordexp.c (wordexp): Don't leak file streams.

View File

@ -37,9 +37,12 @@ wordexp(const char *words, wordexp_t *pwordexp, int flags)
char *iter; char *iter;
pid_t pid; pid_t pid;
int num_words = 0; int num_words = 0;
int num_bytes = 0;
int fd[2]; int fd[2];
int fd_err[2]; int fd_err[2];
int err = 0; int err = 0;
char *ewords;
char *eword;
if (pwordexp == NULL) if (pwordexp == NULL)
{ {
@ -122,27 +125,37 @@ wordexp(const char *words, wordexp_t *pwordexp, int flags)
(pwordexp->we_wordc + num_words + offs + 1) * sizeof(char *)))) (pwordexp->we_wordc + num_words + offs + 1) * sizeof(char *))))
return WRDE_NOSPACE; return WRDE_NOSPACE;
/* Get number of bytes required for storage of num_words words. */ /* Get number of bytes required for storage of all num_words words. */
fgets(tmp, MAXLINELEN, f); fgets(tmp, MAXLINELEN, f);
if((iter = strchr(tmp, '\n'))) if((iter = strchr(tmp, '\n')))
*iter = '\0'; *iter = '\0';
/* Get each expansion from the shell output, and store each in num_bytes = atoi(tmp);
pwordexp's we_wordv vector. */
/* Get expansion from the shell output. */
if (!(ewords = (char *)malloc(num_bytes + num_words + 1)))
return WRDE_NOSPACE;
fread(ewords, 1, num_bytes + num_words, f);
ewords[num_bytes + num_words] = 0;
/* Store each entry in pwordexp's we_wordv vector. */
eword = ewords;
for(i = 0; i < num_words; i++) for(i = 0; i < num_words; i++)
{ {
fgets(tmp, MAXLINELEN, f); if (eword && (iter = strchr(eword, '\n')))
if((iter = strchr(tmp, '\n')))
*iter = '\0'; *iter = '\0';
pwordexp->we_wordv[pwordexp->we_wordc + offs + i] = strdup(tmp); if (eword)
eword = strdup(eword);
pwordexp->we_wordv[pwordexp->we_wordc + offs + i] = eword;
eword = iter ? iter + 1 : iter;
} }
pwordexp->we_wordv[pwordexp->we_wordc + offs + i] = NULL; pwordexp->we_wordv[pwordexp->we_wordc + offs + i] = NULL;
pwordexp->we_wordc += num_words; pwordexp->we_wordc += num_words;
free(ewords);
fclose(f); fclose(f);
fclose(f_err); fclose(f_err);