diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2025-05-06 22:32:50 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2025-05-06 22:32:50 -0400 |
| commit | 4a00b7905053b9aac6163d23c520fe1aeaaf7075 (patch) | |
| tree | 886be1dffdeb61506d996fcfa95aed8e2a2f2734 | |
| parent | 46555c558870e8b96f1ce361d74fc404ca13c471 (diff) | |
Check return value
| -rw-r--r-- | src/stdlib/simpleparse.h | 8 |
1 files changed, 4 insertions, 4 deletions
diff --git a/src/stdlib/simpleparse.h b/src/stdlib/simpleparse.h index 1448f74e..86a96ce8 100644 --- a/src/stdlib/simpleparse.h +++ b/src/stdlib/simpleparse.h @@ -40,10 +40,10 @@ typedef struct { parse_type_e type; void *dest; } parse_element_t; #define as_void_star(x) ((void*)x) #define strparse(str, ...) simpleparse(str, sizeof((const void*[]){__VA_ARGS__})/sizeof(void*), (parse_type_e[]){MAP_LIST(_parse_type, __VA_ARGS__)}, (void*[]){MAP_LIST(as_void_star, __VA_ARGS__)}) -#define fparse(file, ...) ({ char *_file_contents = NULL; size_t _file_len; \ - (void)getdelim(&_file_contents, &_file_len, '\0', file); \ - const char *_parse_err = strparse(_file_contents, __VA_ARGS__); \ - free(_file_contents); \ +#define fparse(file, ...) ({ char *_file_contents = NULL; size_t _capacity; \ + ssize_t _just_read = getdelim(&_file_contents, &_capacity, '\0', file); \ + const char *_parse_err = _just_read > 0 ? strparse(_file_contents, __VA_ARGS__) : "No such file"; \ + if (_file_contents) free(_file_contents); \ _parse_err; }) const char *simpleparse(const char *str, int n, parse_type_e types[n], void *destinations[n]); |
