Skip to content

Commit

Permalink
Mock - update func signatures for latest glibc (#9117)
Browse files Browse the repository at this point in the history
glibc 2.38 includes strlcpy and strlcat, attempt to detect them before use
  • Loading branch information
mcspr authored Jul 30, 2024
1 parent 05f05d0 commit 3a5157e
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 119 deletions.
15 changes: 4 additions & 11 deletions cores/esp8266/core_esp8266_noniso.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,12 @@
#include <stdint.h>
#include <math.h>
#include <limits>

#include "stdlib_noniso.h"

extern "C" {

char* ltoa(long value, char* result, int base) {
return itoa((int)value, result, base);
}

char* ultoa(unsigned long value, char* result, int base) {
return utoa((unsigned int)value, result, base);
}

char * dtostrf(double number, signed char width, unsigned char prec, char *s) {
char* dtostrf(double number, signed char width, unsigned char prec, char *s) noexcept {
bool negative = false;

if (isnan(number)) {
Expand Down Expand Up @@ -125,7 +118,7 @@ char * dtostrf(double number, signed char width, unsigned char prec, char *s) {
*/
const char* strrstr(const char*__restrict p_pcString,
const char*__restrict p_pcPattern)
const char*__restrict p_pcPattern) noexcept
{
const char* pcResult = 0;

Expand All @@ -149,4 +142,4 @@ const char* strrstr(const char*__restrict p_pcString,
return pcResult;
}

};
} // extern "C"
16 changes: 14 additions & 2 deletions cores/esp8266/stdlib_noniso.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@

#include "stdlib_noniso.h"

extern "C" {

// ulltoa() is slower than std::to_char() (1.6 times)
// but is smaller by ~800B/flash and ~250B/rodata

// ulltoa fills str backwards and can return a pointer different from str
char* ulltoa(unsigned long long val, char* str, int slen, unsigned int radix)
char* ulltoa(unsigned long long val, char* str, int slen, unsigned int radix) noexcept
{
str += --slen;
*str = 0;
Expand All @@ -39,7 +41,7 @@ char* ulltoa(unsigned long long val, char* str, int slen, unsigned int radix)
}

// lltoa fills str backwards and can return a pointer different from str
char* lltoa (long long val, char* str, int slen, unsigned int radix)
char* lltoa(long long val, char* str, int slen, unsigned int radix) noexcept
{
bool neg;
if (val < 0)
Expand All @@ -60,3 +62,13 @@ char* lltoa (long long val, char* str, int slen, unsigned int radix)
}
return ret;
}

char* ltoa(long value, char* result, int base) noexcept {
return itoa((int)value, result, base);
}

char* ultoa(unsigned long value, char* result, int base) noexcept {
return utoa((unsigned int)value, result, base);
}

} // extern "C"
35 changes: 16 additions & 19 deletions cores/esp8266/stdlib_noniso.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,38 +22,35 @@
#ifndef STDLIB_NONISO_H
#define STDLIB_NONISO_H

#include <stdlib.h>

#ifdef __cplusplus
extern "C"{
extern "C" {
#endif

int atoi(const char *s);

long atol(const char* s);

double atof(const char* s);

char* itoa (int val, char *s, int radix);

char* ltoa (long val, char *s, int radix);
#ifdef __cplusplus
#define __STDLIB_NONISO_NOEXCEPT noexcept
#else
#define __STDLIB_NONISO_NOEXCEPT
#endif

char* lltoa (long long val, char* str, int slen, unsigned int radix);
char* ltoa (long val, char *s, int radix) __STDLIB_NONISO_NOEXCEPT;

char* utoa (unsigned int val, char *s, int radix);
char* lltoa (long long val, char* str, int slen, unsigned int radix) __STDLIB_NONISO_NOEXCEPT;

char* ultoa (unsigned long val, char *s, int radix);
char* ultoa (unsigned long val, char *s, int radix) __STDLIB_NONISO_NOEXCEPT;

char* ulltoa (unsigned long long val, char* str, int slen, unsigned int radix);
char* ulltoa (unsigned long long val, char* str, int slen, unsigned int radix) __STDLIB_NONISO_NOEXCEPT;

char* dtostrf (double val, signed char width, unsigned char prec, char *s);
char* dtostrf (double val, signed char width, unsigned char prec, char *s) __STDLIB_NONISO_NOEXCEPT;

void reverse(char* begin, char* end);
const char* strrstr (const char*__restrict p_pcString,
const char*__restrict p_pcPattern) __STDLIB_NONISO_NOEXCEPT;

const char* strrstr(const char*__restrict p_pcString,
const char*__restrict p_pcPattern);
#undef __STDLIB_NONISO_NOEXCEPT

#ifdef __cplusplus
} // extern "C"
#endif


#endif
26 changes: 26 additions & 0 deletions tests/host/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ GENHTML ?= genhtml
CXXFLAGS += -std=gnu++17
CFLAGS += -std=gnu17

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

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

$(shell mkdir -p $(BINDIR))

# Core files sometimes override libc functions, check when necessary to hide them
# TODO proper configure script / other build system?
ifeq (,$(wildcard $(BINDIR)/.have_strlcpy))
$(shell echo -e '#include <cstring>\nint main(){char a[4]{}; char b[4]{}; strlcpy(&a[0], &b[0], sizeof(a)); return 0;}' | \
$(CXX) -x c++ - -o $(BINDIR)/.have_strlcpy 2>/dev/null || ( echo -e '#!/bin/sh\nexit 1' > $(BINDIR)/.have_strlcpy ; chmod +x $(BINDIR)/.have_strlcpy; ))
endif

$(shell $(BINDIR)/.have_strlcpy)
ifneq ($(.SHELLSTATUS), 0)
FLAGS += -DSTRLCPY_MISSING
endif

ifeq (,$(wildcard $(BINDIR)/.have_strlcat))
$(shell echo -e '#include <cstring>\nint main(){char a[4]{}; strlcat(&a[0], "test", sizeof(a)); return 0;}' | \
$(CXX) -x c++ - -o $(BINDIR)/.have_strlcat 2>/dev/null || ( echo -e '#!/bin/sh\nexit 1' > $(BINDIR)/.have_strlcat ; chmod +x $(BINDIR)/.have_strlcat; ))
endif

$(shell $(BINDIR)/.have_strlcat)
ifneq ($(.SHELLSTATUS), 0)
FLAGS += -DSTRLCAT_MISSING
endif

# Actual build recipes

CORE_CPP_FILES := \
$(addprefix $(abspath $(CORE_PATH))/,\
debug.cpp \
Expand Down
15 changes: 10 additions & 5 deletions tests/host/common/mock.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,23 @@
#define D8 8

#include <stddef.h>
#include <stdlib.h>
#include <string.h>

#include <stdlib_noniso.h>

#ifdef __cplusplus
extern "C"
{
#endif
// TODO: #include <stdlib_noniso.h> ?
char* itoa(int val, char* s, int radix);
char* ltoa(long val, char* s, int radix);

char* utoa(unsigned value, char* result, int base);
char* itoa(int value, char* result, int base);
#ifdef STRLCAT_MISSING
size_t strlcat(char* dst, const char* src, size_t size);
#endif
#ifdef STRLCPY_MISSING
size_t strlcpy(char* dst, const char* src, size_t size);

#endif
#ifdef __cplusplus
}
#endif
Expand Down
22 changes: 3 additions & 19 deletions tests/host/common/noniso.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
#include <stdbool.h>
#include <stdint.h>
#include <math.h>
#include "stdlib_noniso.h"

void reverse(char* begin, char* end)
#include <stdlib_noniso.h>

static void reverse(char* begin, char* end)
{
char* is = begin;
char* ie = end - 1;
Expand Down Expand Up @@ -84,20 +85,3 @@ char* itoa(int value, char* result, int base)
utoa(uvalue, result, base);
return out;
}

int atoi(const char* s)
{
return (int)atol(s);
}

long atol(const char* s)
{
char* tmp;
return strtol(s, &tmp, 10);
}

double atof(const char* s)
{
char* tmp;
return strtod(s, &tmp);
}
118 changes: 56 additions & 62 deletions tests/host/common/strl.cpp
Original file line number Diff line number Diff line change
@@ -1,84 +1,78 @@
// https://gist.github.com/Fonger/98cc95ac39fbe1a7e4d9

#ifndef HAVE_STRLCAT
/*
'_cups_strlcat()' - Safely concatenate two strings.
*/

size_t /* O - Length of string */
strlcat(char* dst, /* O - Destination string */
const char* src, /* I - Source string */
size_t size) /* I - Size of destination string buffer */
#include <cstddef>
#include <cstdlib>
#include <cstring>

extern "C"
{
size_t srclen; /* Length of source string */
size_t dstlen; /* Length of destination string */
#ifdef STRLCAT_MISSING
// '_cups_strlcat()' - Safely concatenate two strings.

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

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

if (!size)
{
return (dstlen); /* No room, return immediately... */
}
dstlen = strlen(dst);
size -= dstlen + 1;

/*
Figure out how much room is needed...
*/
if (!size)
{
return (dstlen); /* No room, return immediately... */
}

srclen = strlen(src);
// Figure out how much room is needed...

/*
Copy the appropriate amount...
*/
srclen = strlen(src);

if (srclen > size)
{
srclen = size;
// Copy the appropriate amount...

if (srclen > size)
{
srclen = size;
}

memcpy(dst + dstlen, src, srclen);
dst[dstlen + srclen] = '\0';

return (dstlen + srclen);
}
#endif /* STRLCAT_MISSING */

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

return (dstlen + srclen);
}
#endif /* !HAVE_STRLCAT */
size_t /* O - Length of string */
strlcpy(char* dst, /* O - Destination string */
const char* src, /* I - Source string */
size_t size) /* I - Size of destination string buffer */
{
size_t srclen; /* Length of source string */

#ifndef HAVE_STRLCPY
/*
'_cups_strlcpy()' - Safely copy two strings.
*/
// Figure out how much room is needed...

size_t /* O - Length of string */
strlcpy(char* dst, /* O - Destination string */
const char* src, /* I - Source string */
size_t size) /* I - Size of destination string buffer */
{
size_t srclen; /* Length of source string */
size--;

/*
Figure out how much room is needed...
*/
srclen = strlen(src);

size--;
// Copy the appropriate amount...

srclen = strlen(src);
if (srclen > size)
{
srclen = size;
}

/*
Copy the appropriate amount...
*/
memcpy(dst, src, srclen);
dst[srclen] = '\0';

if (srclen > size)
{
srclen = size;
return (srclen);
}
#endif /* STRLCPY_MISSING */

memcpy(dst, src, srclen);
dst[srclen] = '\0';

return (srclen);
}
#endif /* !HAVE_STRLCPY */
} // extern "C"
1 change: 0 additions & 1 deletion tools/sdk/include/ets_sys.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,6 @@ void *ets_memset(void *s, int c, size_t n);
void ets_timer_arm_new(ETSTimer *a, int b, int c, int isMstimer);
void ets_timer_setfn(ETSTimer *t, ETSTimerFunc *fn, void *parg);
void ets_timer_disarm(ETSTimer *a);
int atoi(const char *nptr);
int ets_strncmp(const char *s1, const char *s2, int len);
int ets_strcmp(const char *s1, const char *s2);
int ets_strlen(const char *s);
Expand Down

0 comments on commit 3a5157e

Please sign in to comment.