Files
RedBear-OS/tests/error.c
T
Josh Megnauth 1d5179eb71 Const calculate buffer size for strerror
The current buffer size is hard set to 256. Using const evaluation, the
maximum buf size can be calculated at compile time.
2025-07-10 03:18:17 +00:00

53 lines
1.4 KiB
C

#include <err.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "test_helpers.h"
int main(void) {
chdir("nonexistent");
int err = errno;
printf("errno: %d = %s\n", err, strerror(errno));
perror("perror");
char buf1[256] = {0};
int ret1 = strerror_r(err, buf1, 256);
printf("errno: %d = %s, return: %d\n", err, buf1, ret1);
char buf2[3] = {0};
int ret2 = strerror_r(err, buf2, 3);
printf("errno: %d = %s, return: %d\n", err, buf2, ret2);
char buf3[256] = {0};
int ret3 = strerror_r(err, buf3, 0);
printf("errno: %d = %s, return: %d\n", err, buf3, ret3);
// Test that the statically allocated buffer doesn't overflow
for (int code = EPERM; code < ENOTRECOVERABLE; ++code) {
const char* message = strerror(code);
if (!message) {
errx(EXIT_FAILURE,
"Expected message for error code: %d",
code
);
}
}
const char* actual_error = strerror(INT_MAX);
char expected_error[256] = {0};
sprintf(expected_error, "Unknown error %d", INT_MAX);
if (strncmp(expected_error, actual_error, 25) != 0) {
errx(EXIT_FAILURE,
"Expected: %s\nGot: %s",
expected_error, actual_error
);
}
return EXIT_SUCCESS;
}