Skip to content

Commit 3a5157e

Browse files
authored
Mock - update func signatures for latest glibc (#9117)
glibc 2.38 includes strlcpy and strlcat, attempt to detect them before use
1 parent 05f05d0 commit 3a5157e

File tree

8 files changed

+129
-119
lines changed

8 files changed

+129
-119
lines changed

cores/esp8266/core_esp8266_noniso.cpp

+4-11
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,12 @@
2828
#include <stdint.h>
2929
#include <math.h>
3030
#include <limits>
31+
3132
#include "stdlib_noniso.h"
3233

3334
extern "C" {
3435

35-
char* ltoa(long value, char* result, int base) {
36-
return itoa((int)value, result, base);
37-
}
38-
39-
char* ultoa(unsigned long value, char* result, int base) {
40-
return utoa((unsigned int)value, result, base);
41-
}
42-
43-
char * dtostrf(double number, signed char width, unsigned char prec, char *s) {
36+
char* dtostrf(double number, signed char width, unsigned char prec, char *s) noexcept {
4437
bool negative = false;
4538

4639
if (isnan(number)) {
@@ -125,7 +118,7 @@ char * dtostrf(double number, signed char width, unsigned char prec, char *s) {
125118
126119
*/
127120
const char* strrstr(const char*__restrict p_pcString,
128-
const char*__restrict p_pcPattern)
121+
const char*__restrict p_pcPattern) noexcept
129122
{
130123
const char* pcResult = 0;
131124

@@ -149,4 +142,4 @@ const char* strrstr(const char*__restrict p_pcString,
149142
return pcResult;
150143
}
151144

152-
};
145+
} // extern "C"

cores/esp8266/stdlib_noniso.cpp

+14-2
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@
2121

2222
#include "stdlib_noniso.h"
2323

24+
extern "C" {
25+
2426
// ulltoa() is slower than std::to_char() (1.6 times)
2527
// but is smaller by ~800B/flash and ~250B/rodata
2628

2729
// ulltoa fills str backwards and can return a pointer different from str
28-
char* ulltoa(unsigned long long val, char* str, int slen, unsigned int radix)
30+
char* ulltoa(unsigned long long val, char* str, int slen, unsigned int radix) noexcept
2931
{
3032
str += --slen;
3133
*str = 0;
@@ -39,7 +41,7 @@ char* ulltoa(unsigned long long val, char* str, int slen, unsigned int radix)
3941
}
4042

4143
// lltoa fills str backwards and can return a pointer different from str
42-
char* lltoa (long long val, char* str, int slen, unsigned int radix)
44+
char* lltoa(long long val, char* str, int slen, unsigned int radix) noexcept
4345
{
4446
bool neg;
4547
if (val < 0)
@@ -60,3 +62,13 @@ char* lltoa (long long val, char* str, int slen, unsigned int radix)
6062
}
6163
return ret;
6264
}
65+
66+
char* ltoa(long value, char* result, int base) noexcept {
67+
return itoa((int)value, result, base);
68+
}
69+
70+
char* ultoa(unsigned long value, char* result, int base) noexcept {
71+
return utoa((unsigned int)value, result, base);
72+
}
73+
74+
} // extern "C"

cores/esp8266/stdlib_noniso.h

+16-19
Original file line numberDiff line numberDiff line change
@@ -22,38 +22,35 @@
2222
#ifndef STDLIB_NONISO_H
2323
#define STDLIB_NONISO_H
2424

25+
#include <stdlib.h>
26+
2527
#ifdef __cplusplus
26-
extern "C"{
28+
extern "C" {
2729
#endif
2830

29-
int atoi(const char *s);
30-
31-
long atol(const char* s);
32-
33-
double atof(const char* s);
34-
35-
char* itoa (int val, char *s, int radix);
36-
37-
char* ltoa (long val, char *s, int radix);
31+
#ifdef __cplusplus
32+
#define __STDLIB_NONISO_NOEXCEPT noexcept
33+
#else
34+
#define __STDLIB_NONISO_NOEXCEPT
35+
#endif
3836

39-
char* lltoa (long long val, char* str, int slen, unsigned int radix);
37+
char* ltoa (long val, char *s, int radix) __STDLIB_NONISO_NOEXCEPT;
4038

41-
char* utoa (unsigned int val, char *s, int radix);
39+
char* lltoa (long long val, char* str, int slen, unsigned int radix) __STDLIB_NONISO_NOEXCEPT;
4240

43-
char* ultoa (unsigned long val, char *s, int radix);
41+
char* ultoa (unsigned long val, char *s, int radix) __STDLIB_NONISO_NOEXCEPT;
4442

45-
char* ulltoa (unsigned long long val, char* str, int slen, unsigned int radix);
43+
char* ulltoa (unsigned long long val, char* str, int slen, unsigned int radix) __STDLIB_NONISO_NOEXCEPT;
4644

47-
char* dtostrf (double val, signed char width, unsigned char prec, char *s);
45+
char* dtostrf (double val, signed char width, unsigned char prec, char *s) __STDLIB_NONISO_NOEXCEPT;
4846

49-
void reverse(char* begin, char* end);
47+
const char* strrstr (const char*__restrict p_pcString,
48+
const char*__restrict p_pcPattern) __STDLIB_NONISO_NOEXCEPT;
5049

51-
const char* strrstr(const char*__restrict p_pcString,
52-
const char*__restrict p_pcPattern);
50+
#undef __STDLIB_NONISO_NOEXCEPT
5351

5452
#ifdef __cplusplus
5553
} // extern "C"
5654
#endif
5755

58-
5956
#endif

tests/host/Makefile

+26
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ GENHTML ?= genhtml
3030
CXXFLAGS += -std=gnu++17
3131
CFLAGS += -std=gnu17
3232

33+
# 32-bit mode is prefered, but not required
3334
ifeq ($(FORCE32),1)
3435
SIZEOFLONG = $(shell echo 'int main(){return sizeof(long);}'|$(CXX) -m32 -x c++ - -o sizeoflong 2>/dev/null && ./sizeoflong; echo $$?; rm -f sizeoflong;)
3536
ifneq ($(SIZEOFLONG),4)
@@ -50,6 +51,7 @@ endif
5051
OUTPUT_BINARY := $(BINDIR)/host_tests
5152
LCOV_DIRECTORY := $(BINDIR)/../lcov
5253

54+
# Hide full build commands by default
5355
ifeq ($(V), 0)
5456
VERBC = @echo "C $@";
5557
VERBCXX = @echo "C++ $@";
@@ -66,6 +68,30 @@ endif
6668

6769
$(shell mkdir -p $(BINDIR))
6870

71+
# Core files sometimes override libc functions, check when necessary to hide them
72+
# TODO proper configure script / other build system?
73+
ifeq (,$(wildcard $(BINDIR)/.have_strlcpy))
74+
$(shell echo -e '#include <cstring>\nint main(){char a[4]{}; char b[4]{}; strlcpy(&a[0], &b[0], sizeof(a)); return 0;}' | \
75+
$(CXX) -x c++ - -o $(BINDIR)/.have_strlcpy 2>/dev/null || ( echo -e '#!/bin/sh\nexit 1' > $(BINDIR)/.have_strlcpy ; chmod +x $(BINDIR)/.have_strlcpy; ))
76+
endif
77+
78+
$(shell $(BINDIR)/.have_strlcpy)
79+
ifneq ($(.SHELLSTATUS), 0)
80+
FLAGS += -DSTRLCPY_MISSING
81+
endif
82+
83+
ifeq (,$(wildcard $(BINDIR)/.have_strlcat))
84+
$(shell echo -e '#include <cstring>\nint main(){char a[4]{}; strlcat(&a[0], "test", sizeof(a)); return 0;}' | \
85+
$(CXX) -x c++ - -o $(BINDIR)/.have_strlcat 2>/dev/null || ( echo -e '#!/bin/sh\nexit 1' > $(BINDIR)/.have_strlcat ; chmod +x $(BINDIR)/.have_strlcat; ))
86+
endif
87+
88+
$(shell $(BINDIR)/.have_strlcat)
89+
ifneq ($(.SHELLSTATUS), 0)
90+
FLAGS += -DSTRLCAT_MISSING
91+
endif
92+
93+
# Actual build recipes
94+
6995
CORE_CPP_FILES := \
7096
$(addprefix $(abspath $(CORE_PATH))/,\
7197
debug.cpp \

tests/host/common/mock.h

+10-5
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,23 @@
5656
#define D8 8
5757

5858
#include <stddef.h>
59+
#include <stdlib.h>
60+
#include <string.h>
61+
62+
#include <stdlib_noniso.h>
5963

6064
#ifdef __cplusplus
6165
extern "C"
6266
{
6367
#endif
64-
// TODO: #include <stdlib_noniso.h> ?
65-
char* itoa(int val, char* s, int radix);
66-
char* ltoa(long val, char* s, int radix);
67-
68+
char* utoa(unsigned value, char* result, int base);
69+
char* itoa(int value, char* result, int base);
70+
#ifdef STRLCAT_MISSING
6871
size_t strlcat(char* dst, const char* src, size_t size);
72+
#endif
73+
#ifdef STRLCPY_MISSING
6974
size_t strlcpy(char* dst, const char* src, size_t size);
70-
75+
#endif
7176
#ifdef __cplusplus
7277
}
7378
#endif

tests/host/common/noniso.c

+3-19
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818
#include <stdbool.h>
1919
#include <stdint.h>
2020
#include <math.h>
21-
#include "stdlib_noniso.h"
2221

23-
void reverse(char* begin, char* end)
22+
#include <stdlib_noniso.h>
23+
24+
static void reverse(char* begin, char* end)
2425
{
2526
char* is = begin;
2627
char* ie = end - 1;
@@ -84,20 +85,3 @@ char* itoa(int value, char* result, int base)
8485
utoa(uvalue, result, base);
8586
return out;
8687
}
87-
88-
int atoi(const char* s)
89-
{
90-
return (int)atol(s);
91-
}
92-
93-
long atol(const char* s)
94-
{
95-
char* tmp;
96-
return strtol(s, &tmp, 10);
97-
}
98-
99-
double atof(const char* s)
100-
{
101-
char* tmp;
102-
return strtod(s, &tmp);
103-
}

tests/host/common/strl.cpp

+56-62
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,78 @@
11
// https://gist.github.com/Fonger/98cc95ac39fbe1a7e4d9
22

3-
#ifndef HAVE_STRLCAT
4-
/*
5-
'_cups_strlcat()' - Safely concatenate two strings.
6-
*/
7-
8-
size_t /* O - Length of string */
9-
strlcat(char* dst, /* O - Destination string */
10-
const char* src, /* I - Source string */
11-
size_t size) /* I - Size of destination string buffer */
3+
#include <cstddef>
4+
#include <cstdlib>
5+
#include <cstring>
6+
7+
extern "C"
128
{
13-
size_t srclen; /* Length of source string */
14-
size_t dstlen; /* Length of destination string */
9+
#ifdef STRLCAT_MISSING
10+
// '_cups_strlcat()' - Safely concatenate two strings.
1511

16-
/*
17-
Figure out how much room is left...
18-
*/
12+
size_t /* O - Length of string */
13+
strlcat(char* dst, /* O - Destination string */
14+
const char* src, /* I - Source string */
15+
size_t size) /* I - Size of destination string buffer */
16+
{
17+
size_t srclen; /* Length of source string */
18+
size_t dstlen; /* Length of destination string */
1919

20-
dstlen = strlen(dst);
21-
size -= dstlen + 1;
20+
// Figure out how much room is left...
2221

23-
if (!size)
24-
{
25-
return (dstlen); /* No room, return immediately... */
26-
}
22+
dstlen = strlen(dst);
23+
size -= dstlen + 1;
2724

28-
/*
29-
Figure out how much room is needed...
30-
*/
25+
if (!size)
26+
{
27+
return (dstlen); /* No room, return immediately... */
28+
}
3129

32-
srclen = strlen(src);
30+
// Figure out how much room is needed...
3331

34-
/*
35-
Copy the appropriate amount...
36-
*/
32+
srclen = strlen(src);
3733

38-
if (srclen > size)
39-
{
40-
srclen = size;
34+
// Copy the appropriate amount...
35+
36+
if (srclen > size)
37+
{
38+
srclen = size;
39+
}
40+
41+
memcpy(dst + dstlen, src, srclen);
42+
dst[dstlen + srclen] = '\0';
43+
44+
return (dstlen + srclen);
4145
}
46+
#endif /* STRLCAT_MISSING */
4247

43-
memcpy(dst + dstlen, src, srclen);
44-
dst[dstlen + srclen] = '\0';
48+
#ifdef STRLCPY_MISSING
49+
// '_cups_strlcpy()' - Safely copy two strings.
4550

46-
return (dstlen + srclen);
47-
}
48-
#endif /* !HAVE_STRLCAT */
51+
size_t /* O - Length of string */
52+
strlcpy(char* dst, /* O - Destination string */
53+
const char* src, /* I - Source string */
54+
size_t size) /* I - Size of destination string buffer */
55+
{
56+
size_t srclen; /* Length of source string */
4957

50-
#ifndef HAVE_STRLCPY
51-
/*
52-
'_cups_strlcpy()' - Safely copy two strings.
53-
*/
58+
// Figure out how much room is needed...
5459

55-
size_t /* O - Length of string */
56-
strlcpy(char* dst, /* O - Destination string */
57-
const char* src, /* I - Source string */
58-
size_t size) /* I - Size of destination string buffer */
59-
{
60-
size_t srclen; /* Length of source string */
60+
size--;
6161

62-
/*
63-
Figure out how much room is needed...
64-
*/
62+
srclen = strlen(src);
6563

66-
size--;
64+
// Copy the appropriate amount...
6765

68-
srclen = strlen(src);
66+
if (srclen > size)
67+
{
68+
srclen = size;
69+
}
6970

70-
/*
71-
Copy the appropriate amount...
72-
*/
71+
memcpy(dst, src, srclen);
72+
dst[srclen] = '\0';
7373

74-
if (srclen > size)
75-
{
76-
srclen = size;
74+
return (srclen);
7775
}
76+
#endif /* STRLCPY_MISSING */
7877

79-
memcpy(dst, src, srclen);
80-
dst[srclen] = '\0';
81-
82-
return (srclen);
83-
}
84-
#endif /* !HAVE_STRLCPY */
78+
} // extern "C"

tools/sdk/include/ets_sys.h

-1
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,6 @@ void *ets_memset(void *s, int c, size_t n);
208208
void ets_timer_arm_new(ETSTimer *a, int b, int c, int isMstimer);
209209
void ets_timer_setfn(ETSTimer *t, ETSTimerFunc *fn, void *parg);
210210
void ets_timer_disarm(ETSTimer *a);
211-
int atoi(const char *nptr);
212211
int ets_strncmp(const char *s1, const char *s2, int len);
213212
int ets_strcmp(const char *s1, const char *s2);
214213
int ets_strlen(const char *s);

0 commit comments

Comments
 (0)