Correct behaviour on 64-byte ints?

This commit is contained in:
Neil 2023-05-06 23:00:49 -07:00
parent 5cb731c9cc
commit c0228d87bc
1 changed files with 5 additions and 2 deletions

View File

@ -4,6 +4,7 @@
#include "text.h"
#include <stdio.h>
#include <limits.h>
#include <stdint.h>
#define ARRAY_NAME char
#define ARRAY_TYPE char
@ -30,11 +31,13 @@ char *text_append_file(struct char_array *text, const char *const fn) {
start = text->size;
if(!(fp = fopen(fn, "r"))) goto catch;
/* Read entire file in chunks. We don't allow any text file to go over
`INT_MAX` because `printf("%.*s", int, char *)` is used. */
`INT_MAX` and `UINT32_MAX` because `printf("%.*s", int, char *)` is used
and we store it in an `uint32_t`. */
do if(!(cursor = char_array_buffer(text, granularity))
|| (nread = fread(cursor, 1, granularity, fp), ferror(fp))
|| !char_array_append(text, nread)
|| text->size - start >= INT_MAX) goto catch;
|| text->size - start >= (INT_MAX < UINT32_MAX ? INT_MAX : UINT32_MAX))
goto catch;
while(nread == granularity);
/* File to `C` string. */
if(!(cursor = char_array_new(text))) goto catch;