Skip to content

Commit cd729b9

Browse files
committed
Avoid calling library functions in __libc_fatal
These might call __libc_fatal recursively Also fixes a long standing bug, using sizeof(ptr) when writing the message.
1 parent 5e7d630 commit cd729b9

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

mintlib/libc_fatal.c

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,36 @@
1212
#include <stdlib.h>
1313
#include <unistd.h>
1414
#include <string.h>
15+
#include <mint/osbind.h>
1516
#include "lib.h"
1617

18+
#ifdef __OPTIMIZE__
19+
#pragma GCC optimize "-Os"
20+
#endif
21+
1722
/* Abort with an error message.
1823
This function should write MESSAGE out in the most reliable way.
1924
It is called in situations like internal stdio lossage. */
2025

2126
void
22-
__libc_fatal (register const char* message)
27+
__libc_fatal (const char* message)
2328
{
2429
size_t msglen = strlen (message);
25-
register size_t i;
26-
char* crlf = "\r\n";
27-
char* colon = ": fatal libc error: ";
30+
size_t i;
31+
static char const crlf[] = "\r\n";
32+
static char const colon[] = ": fatal libc error: ";
2833

29-
__write (STDERR_FILENO, program_invocation_name,
30-
strlen (program_invocation_name));
31-
__write (STDERR_FILENO, colon, sizeof colon - 1);
34+
Fwrite(STDERR_FILENO, strlen (program_invocation_name), program_invocation_name);
35+
Fwrite(STDERR_FILENO, sizeof(colon) - 1, colon);
3236

3337
/* We better write a CRLF sequence here. Of course this will make
3438
the function slower but we want the user to be able to read
3539
the message under all circumstances. */
3640
for (i = 0; i < msglen; i++) {
3741
if (message[i] == '\n')
38-
__write (STDERR_FILENO, crlf, 2);
42+
Fwrite(STDERR_FILENO, sizeof(crlf) - 1, crlf);
3943
else
40-
__write (STDERR_FILENO, (char*) message + i, 1);
44+
Fwrite(STDERR_FILENO, 1, message + i);
4145
}
4246

4347
abort ();

0 commit comments

Comments
 (0)