1
0
mirror of https://gitlab.com/80486DX2-66/gists synced 2024-12-27 14:50:23 +05:30

freadln.*: fix error handling logic

This commit is contained in:
Intel A80486DX2-66 2024-03-10 15:25:59 +03:00
parent 18af3053f3
commit 3638c9eb7c
Signed by: 80486DX2-66
GPG Key ID: 83631EF27054609B
2 changed files with 7 additions and 5 deletions

View File

@ -51,7 +51,8 @@ int freadln(FILE* f, char** output, size_t* length_out) {
if ((freadln_length_type) (length + 2) <= (freadln_length_type) length) if ((freadln_length_type) (length + 2) <= (freadln_length_type) length)
{ {
errno = ERANGE; errno = ERANGE;
freadln_success_epilogue; freadln_epilogue;
return freadln_ERROR;
} else } else
length++; length++;
@ -61,14 +62,16 @@ int freadln(FILE* f, char** output, size_t* length_out) {
// has already been accumulated. // has already been accumulated.
if (temp == NULL) { if (temp == NULL) {
// keep errno; // keep errno;
freadln_success_epilogue; freadln_epilogue;
return freadln_ERROR;
} }
*output = temp; *output = temp;
} }
errno = 0; errno = 0;
freadln_success_epilogue; freadln_epilogue;
return freadln_OK;
} }
#ifdef TEST #ifdef TEST

View File

@ -19,11 +19,10 @@ enum freadln_status {
freadln_ERROR freadln_ERROR
}; };
#define freadln_success_epilogue do { \ #define freadln_epilogue do { \
(*output)[length] = '\0'; \ (*output)[length] = '\0'; \
if (length_out != NULL) \ if (length_out != NULL) \
*length_out = length; \ *length_out = length; \
return freadln_OK; \
} while (0) } while (0)
int freadln(FILE* f, char** output, size_t* length_out); int freadln(FILE* f, char** output, size_t* length_out);