Skip to content

Commit

Permalink
Fix getdelim and readlink override
Browse files Browse the repository at this point in the history
IgorTodorovskiIBM committed Jul 16, 2024
1 parent 7d497c6 commit 8253dfe
Showing 2 changed files with 42 additions and 38 deletions.
10 changes: 7 additions & 3 deletions src/zos-bpx.cc
Original file line number Diff line number Diff line change
@@ -98,9 +98,13 @@ ssize_t __readlink(const char *path, char *buf, size_t bufsiz) {
return -1;
}

buf[len] = '\0'; // readlink doesn't null terminate

if (buf[0] == '$' || (len > 1 && buf[0] == '/' && buf[1] == '$')) {
if ((len > 0 buf[0] == '$') || (len > 1 && buf[0] == '/' && buf[1] == '$')) {
// Not sure if this is possible, but double check in case:
if (len < bufsiz) {
buf[len] = '\0';
} else {
buf[bufsiz - 1] = '\0';
}
char resolved_path[PATH_MAX];
if (realpath(path, resolved_path) == NULL) {
return -1;
70 changes: 35 additions & 35 deletions src/zos-io.cc
Original file line number Diff line number Diff line change
@@ -1142,50 +1142,50 @@ ssize_t getline(char **lineptr, size_t *n, FILE *stream) {
}

ssize_t getdelim(char **lineptr, size_t *n, int delimiter, FILE *stream) {
char *buf = *lineptr;
size_t bufsize = *n;
size_t len = 0;
int c;
size_t pos;
int c;

if (buf == NULL || bufsize == 0) {
bufsize = 128;
buf = (char*)malloc(bufsize);
if (buf == NULL) {
return -1;
if (lineptr == NULL || n == NULL || stream == NULL) {
errno = EINVAL;
return -1;
}
*lineptr = buf;
*n = bufsize;
}

while (1) {
c = fgetc(stream);
if (c == EOF || c == delimiter) {
break;
if (*lineptr == NULL) {
*n = 128; // Start with a reasonable buffer size
*lineptr = (char *)malloc(*n);
if (*lineptr == NULL) {
errno = ENOMEM;
return -1;
}
}

if (len + 1 >= bufsize) {
bufsize *= 2;
buf = (char*)realloc(buf, bufsize);
if (buf == NULL) {
return -1;
}
*lineptr = buf;
*n = bufsize;
}
pos = 0;

buf[len++] = c;
}
while ((c = fgetc(stream)) != EOF) {
if (pos + 1 >= *n) {
size_t new_size = *n * 2;
char *new_ptr = (char *)realloc(*lineptr, new_size);
if (new_ptr == NULL) {
errno = ENOMEM;
return -1;
}
*lineptr = new_ptr;
*n = new_size;
}

buf[len] = '\0';
(*lineptr)[pos++] = (char)c;

if (c == EOF && len == 0) {
free(buf);
*lineptr = NULL;
*n = 0;
return -1;
}
if (c == delimiter) {
break;
}
}

return len;
if (pos == 0 && c == EOF) {
return -1;
}

(*lineptr)[pos] = '\0';
return pos;
}

int mkostemp(char *tmpl, int flags) {

0 comments on commit 8253dfe

Please sign in to comment.