library: Remove dead branch in file2strvec

A while loop used n >=0
A branch inside the loop had a test for n<0
That's never going to happen!
So the branch must go. There is similiar code outside
the branch. Yes, I miss pythons while else here.

Also cleaned up the ***-awful indentation this function had.
No wonder that branch lived for so long there.

References:
 Coverity #99119

Signed-off-by: Craig Small <csmall@dropbear.xyz>
This commit is contained in:
Craig Small 2016-05-14 22:43:19 +10:00
parent 4f6b8c9c60
commit f85d9a2b95

View File

@ -624,42 +624,38 @@ static char** file2strvec(const char* directory, const char* what) {
sprintf(buf, "%s/%s", directory, what); sprintf(buf, "%s/%s", directory, what);
fd = open(buf, O_RDONLY, 0); fd = open(buf, O_RDONLY, 0);
if(fd==-1) return NULL; if(fd==-1)
return NULL;
/* read whole file into a memory buffer, allocating as we go */ /* read whole file into a memory buffer, allocating as we go */
while ((n = read(fd, buf, sizeof buf - 1)) >= 0) { while ((n = read(fd, buf, sizeof buf - 1)) >= 0) {
if (n < (int)(sizeof buf - 1)) if (n < (int)(sizeof buf - 1))
end_of_file = 1; end_of_file = 1;
if (n == 0 && rbuf == 0) { if (n == 0 && rbuf == 0) {
close(fd); close(fd);
return NULL; /* process died between our open and read */ return NULL; /* process died between our open and read */
} }
if (n < 0) { if (end_of_file && (n == 0 || buf[n-1]))/* last read char not null */
if (rbuf) buf[n++] = '\0'; /* so append null-terminator */
free(rbuf); rbuf = xrealloc(rbuf, tot + n); /* allocate more memory */
close(fd); memcpy(rbuf + tot, buf, n); /* copy buffer into it */
return NULL; /* read error */ tot += n; /* increment total byte ctr */
} if (end_of_file)
if (end_of_file && (n == 0 || buf[n-1]))/* last read char not null */ break;
buf[n++] = '\0'; /* so append null-terminator */
rbuf = xrealloc(rbuf, tot + n); /* allocate more memory */
memcpy(rbuf + tot, buf, n); /* copy buffer into it */
tot += n; /* increment total byte ctr */
if (end_of_file)
break;
} }
close(fd); close(fd);
if (n <= 0 && !end_of_file) { if (n <= 0 && !end_of_file) {
if (rbuf) free(rbuf); if (rbuf)
return NULL; /* read error */ free(rbuf);
return NULL; /* read error */
} }
endbuf = rbuf + tot; /* count space for pointers */ endbuf = rbuf + tot; /* count space for pointers */
align = (sizeof(char*)-1) - ((tot + sizeof(char*)-1) & (sizeof(char*)-1)); align = (sizeof(char*)-1) - ((tot + sizeof(char*)-1) & (sizeof(char*)-1));
for (c = 0, p = rbuf; p < endbuf; p++) { for (c = 0, p = rbuf; p < endbuf; p++) {
if (!*p || *p == '\n') if (!*p || *p == '\n')
c += sizeof(char*); c += sizeof(char*);
if (*p == '\n') if (*p == '\n')
*p = 0; *p = 0;
} }
c += sizeof(char*); /* one extra for NULL term */ c += sizeof(char*); /* one extra for NULL term */
@ -669,8 +665,8 @@ static char** file2strvec(const char* directory, const char* what) {
*q++ = p = rbuf; /* point ptrs to the strings */ *q++ = p = rbuf; /* point ptrs to the strings */
endbuf--; /* do not traverse final NUL */ endbuf--; /* do not traverse final NUL */
while (++p < endbuf) while (++p < endbuf)
if (!*p) /* NUL char implies that */ if (!*p) /* NUL char implies that */
*q++ = p+1; /* next string -> next char */ *q++ = p+1; /* next string -> next char */
*q = 0; /* null ptr list terminator */ *q = 0; /* null ptr list terminator */
return ret; return ret;