Limited loading files to INT_MAX.

This commit is contained in:
Neil 2023-05-06 22:48:13 -07:00
parent 31f41f6ced
commit 5cb731c9cc
2 changed files with 5 additions and 4 deletions

View File

@ -201,8 +201,6 @@ struct journal journal(const char *const dir_journal) {
/* Because it's in a flat array, the pointers are not stable
while we are loading it, and we need to store the offsets. */
*v.offset = (uintptr_t)(contents - j.backing.data);
/* fixme: not representing with int should be a lazy error,
since we use `printf`. */
}
d = 0, int_array_clear(&days);
if(chdir("..") == -1) goto catch;

View File

@ -3,6 +3,7 @@
#include "text.h"
#include <stdio.h>
#include <limits.h>
#define ARRAY_NAME char
#define ARRAY_TYPE char
@ -28,10 +29,12 @@ char *text_append_file(struct char_array *text, const char *const fn) {
assert(text && fn);
start = text->size;
if(!(fp = fopen(fn, "r"))) goto catch;
/* Read entire file in chunks. */
/* Read entire file in chunks. We don't allow any text file to go over
`INT_MAX` because `printf("%.*s", int, char *)` is used. */
do if(!(cursor = char_array_buffer(text, granularity))
|| (nread = fread(cursor, 1, granularity, fp), ferror(fp))
|| !char_array_append(text, nread)) goto catch;
|| !char_array_append(text, nread)
|| text->size - start >= INT_MAX) goto catch;
while(nread == granularity);
/* File to `C` string. */
if(!(cursor = char_array_new(text))) goto catch;