}
ret = close(txt_fd);
}
- /* The +1 is for the additionnal ending \0. See below. */
- if (filesize + 1 > envsize) {
- fprintf(stderr, "The input file is larger than the environment partition size\n");
- return EXIT_FAILURE;
- }
- /* Replace newlines separating variables with \0 */
- for (fp = 0, ep = 0 ; fp < filesize ; fp++) {
+ /* Parse a byte at time until reaching the file OR until the environment fills
+ * up. Check ep against envsize - 1 to allow for extra trailing '\0'. */
+ for (fp = 0, ep = 0 ; fp < filesize && ep < envsize - 1; fp++) {
if (filebuf[fp] == '\n') {
if (fp == 0 || filebuf[fp-1] == '\n') {
/*
envptr[ep++] = filebuf[fp];
}
}
+ /* If there are more bytes in the file still, it means the env filled up
+ * before parsing the whole file. Eat comments & whitespace here to see if
+ * there was anything meaning full left in the file, and if so, throw a error
+ * and exit. */
+ for( ; fp < filesize; fp++ )
+ {
+ if (filebuf[fp] == '\n') {
+ if (fp == 0 || filebuf[fp-1] == '\n') {
+ /* Ignore blank lines */
+ continue;
+ }
+ } else if ((fp == 0 || filebuf[fp-1] == '\n') && filebuf[fp] == '#') {
+ while (++fp < filesize && filebuf[fp] != '\n')
+ continue;
+ } else {
+ fprintf(stderr, "The environment file is too large for the target environment storage\n");
+ return EXIT_FAILURE;
+ }
+ }
/*
* Make sure there is a final '\0'
* And do it again on the next byte to mark the end of the environment.