Skip to content

Commit a0cefc3

Browse files
committed
Correct CRT_degreeSign buffer size & reduce code size
Make "CRT_degreeSign" a writable buffer that will be updated after the initDegreeSign() function. This avoids needing a static buffer in the function. The appropriate buffer size for "CRT_degreeSign" is (MB_LEN_MAX * 2), not 4. One side effect of this change is that "CRT_degreeSign" will lose the const qualifier in the header (CRT.h). Signed-off-by: Kang-Che Sung <[email protected]>
1 parent 42da254 commit a0cefc3

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

CRT.c

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ in the source distribution for its full text.
1212
#include <errno.h>
1313
#include <fcntl.h>
1414
#include <langinfo.h>
15+
#include <limits.h>
1516
#include <signal.h>
1617
#include <stdarg.h>
1718
#include <stdio.h>
@@ -94,21 +95,31 @@ const char* const* CRT_treeStr = CRT_treeStrAscii;
9495

9596
static const Settings* CRT_settings;
9697

97-
const char* CRT_degreeSign;
98+
#ifdef HAVE_LIBNCURSESW
99+
# if MB_LEN_MAX >= 3 // Minimum required to support UTF-8 BMP subset
100+
char CRT_degreeSign[MB_LEN_MAX * 2] = "\xc2\xb0";
101+
# else
102+
char CRT_degreeSign[MB_LEN_MAX * 2] = "";
103+
# endif
104+
#else
105+
char CRT_degreeSign[] = "";
106+
#endif
98107

99-
static const char* initDegreeSign(void) {
108+
static void initDegreeSign(void) {
100109
#ifdef HAVE_LIBNCURSESW
110+
# if MB_LEN_MAX >= 3
101111
if (CRT_utf8)
102-
return "\xc2\xb0";
112+
return;
113+
# endif
103114

104-
static char buffer[4];
105115
// this might fail if the current locale does not support wide characters
106-
int r = snprintf(buffer, sizeof(buffer), "%lc", 176);
107-
if (r > 0)
108-
return buffer;
116+
int r = snprintf(CRT_degreeSign, sizeof(CRT_degreeSign), "%lc", 176);
117+
if (r <= 0)
118+
CRT_degreeSign[0] = '\0';
109119
#endif
110120

111-
return "";
121+
// No-op
122+
return;
112123
}
113124

114125
const int* CRT_colors;
@@ -1145,7 +1156,7 @@ IGNORE_WCASTQUAL_END
11451156

11461157
CRT_setMouse(settings->enableMouse);
11471158

1148-
CRT_degreeSign = initDegreeSign();
1159+
initDegreeSign();
11491160
}
11501161

11511162
void CRT_done(void) {

CRT.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ void CRT_handleSIGSEGV(int signal) ATTR_NORETURN;
180180
#define KEY_FOCUS_OUT (KEY_MAX + 'O')
181181
#define KEY_DEL_MAC 127
182182

183-
extern const char* CRT_degreeSign;
183+
extern char CRT_degreeSign[];
184184

185185
#ifdef HAVE_LIBNCURSESW
186186

0 commit comments

Comments
 (0)