Fix issues with path reading from pipes (don't UTF8 validate chunks,

because they can be fragments) and some misc buffering issues.
This commit is contained in:
Bruce Hill 2024-09-09 15:28:03 -04:00
parent cc94afcc56
commit 02eefdd52c

View File

@ -8,6 +8,7 @@
#include <sys/mman.h> #include <sys/mman.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h>
#include <unistr.h> #include <unistr.h>
#include "array.h" #include "array.h"
@ -257,24 +258,35 @@ public Text_t Path$read(Path_t path)
close(fd); close(fd);
return Text$from_strn(gc_mem, (size_t)sb.st_size); return Text$from_strn(gc_mem, (size_t)sb.st_size);
} else { } else {
const size_t chunk_size = 256; size_t capacity = 256, len = 0;
char *buf = GC_MALLOC_ATOMIC(chunk_size); char *content = GC_MALLOC_ATOMIC(capacity);
Text_t contents = Text(""); for (;;) {
ssize_t just_read; char chunk[256];
do { ssize_t just_read = read(fd, chunk, sizeof(chunk));
just_read = read(fd, buf, chunk_size);
if (just_read < 0) if (just_read < 0)
fail("Failed while reading file: %k (%s)", &path, strerror(errno)); fail("Failed while reading file: %k (%s)", &path, strerror(errno));
else if (just_read == 0) else if (just_read == 0) {
if (errno == EAGAIN || errno == EINTR)
continue;
break; break;
}
if (u8_check((uint8_t*)buf, (size_t)just_read) != NULL) if (len + (size_t)just_read >= capacity) {
fail("File does not contain valid UTF8 data!"); content = GC_REALLOC(content, (capacity *= 2));
contents = Texts(contents, Text$from_strn(buf, (size_t)just_read)); }
buf = GC_MALLOC_ATOMIC(chunk_size);
} while (just_read > 0); memcpy(&content[len], chunk, (size_t)just_read);
len += (size_t)just_read;
if ((size_t)just_read < sizeof(chunk))
break;
}
close(fd); close(fd);
return contents;
if (u8_check((uint8_t*)content, len) != NULL)
fail("File does not contain valid UTF8 data!");
return Text$from_strn(content, len);
} }
} }