From 4eb437050007d01e6efff14d2bac50a830bad009 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Mon, 9 Sep 2024 14:54:50 -0700
Subject: [PATCH] SDL_strtoll(), SDL_strtoull(), SDL_lltoa(), and SDL_ulltoa()
use long long values
---
include/SDL3/SDL_stdinc.h | 42 +++++++++---------
src/SDL_properties.c | 2 +-
src/dynapi/SDL_dynapi_procs.h | 8 ++--
src/joystick/SDL_steam_virtual_gamepad.c | 2 +-
src/stdlib/SDL_string.c | 54 ++++++++++++------------
test/testqsort.c | 2 +-
6 files changed, 52 insertions(+), 58 deletions(-)
diff --git a/include/SDL3/SDL_stdinc.h b/include/SDL3/SDL_stdinc.h
index f2e58a1960fc7..4b7aae2ff9d1f 100644
--- a/include/SDL3/SDL_stdinc.h
+++ b/include/SDL3/SDL_stdinc.h
@@ -570,15 +570,17 @@ typedef Sint64 SDL_Time;
/** \cond */
#ifndef DOXYGEN_SHOULD_IGNORE_THIS
-SDL_COMPILE_TIME_ASSERT(bool, sizeof(SDL_bool) == 1);
-SDL_COMPILE_TIME_ASSERT(uint8, sizeof(Uint8) == 1);
-SDL_COMPILE_TIME_ASSERT(sint8, sizeof(Sint8) == 1);
-SDL_COMPILE_TIME_ASSERT(uint16, sizeof(Uint16) == 2);
-SDL_COMPILE_TIME_ASSERT(sint16, sizeof(Sint16) == 2);
-SDL_COMPILE_TIME_ASSERT(uint32, sizeof(Uint32) == 4);
-SDL_COMPILE_TIME_ASSERT(sint32, sizeof(Sint32) == 4);
-SDL_COMPILE_TIME_ASSERT(uint64, sizeof(Uint64) == 8);
-SDL_COMPILE_TIME_ASSERT(sint64, sizeof(Sint64) == 8);
+SDL_COMPILE_TIME_ASSERT(bool_size, sizeof(SDL_bool) == 1);
+SDL_COMPILE_TIME_ASSERT(uint8_size, sizeof(Uint8) == 1);
+SDL_COMPILE_TIME_ASSERT(sint8_size, sizeof(Sint8) == 1);
+SDL_COMPILE_TIME_ASSERT(uint16_size, sizeof(Uint16) == 2);
+SDL_COMPILE_TIME_ASSERT(sint16_size, sizeof(Sint16) == 2);
+SDL_COMPILE_TIME_ASSERT(uint32_size, sizeof(Uint32) == 4);
+SDL_COMPILE_TIME_ASSERT(sint32_size, sizeof(Sint32) == 4);
+SDL_COMPILE_TIME_ASSERT(uint64_size, sizeof(Uint64) == 8);
+SDL_COMPILE_TIME_ASSERT(sint64_size, sizeof(Sint64) == 8);
+SDL_COMPILE_TIME_ASSERT(uint64_longlong, sizeof(Uint64) <= sizeof(unsigned long long));
+SDL_COMPILE_TIME_ASSERT(size_t_longlong, sizeof(size_t) <= sizeof(unsigned long long));
typedef struct SDL_alignment_test
{
Uint8 a;
@@ -1668,8 +1670,8 @@ extern SDL_DECLSPEC char * SDLCALL SDL_itoa(int value, char *str, int radix);
extern SDL_DECLSPEC char * SDLCALL SDL_uitoa(unsigned int value, char *str, int radix);
extern SDL_DECLSPEC char * SDLCALL SDL_ltoa(long value, char *str, int radix);
extern SDL_DECLSPEC char * SDLCALL SDL_ultoa(unsigned long value, char *str, int radix);
-extern SDL_DECLSPEC char * SDLCALL SDL_lltoa(Sint64 value, char *str, int radix);
-extern SDL_DECLSPEC char * SDLCALL SDL_ulltoa(Uint64 value, char *str, int radix);
+extern SDL_DECLSPEC char * SDLCALL SDL_lltoa(long long value, char *str, int radix);
+extern SDL_DECLSPEC char * SDLCALL SDL_ulltoa(unsigned long long value, char *str, int radix);
/**
* Parse an `int` from a string.
@@ -1784,7 +1786,7 @@ extern SDL_DECLSPEC long SDLCALL SDL_strtol(const char *str, char **endp, int ba
extern SDL_DECLSPEC unsigned long SDLCALL SDL_strtoul(const char *str, char **endp, int base);
/**
- * Parse an Sint64 from a string.
+ * Parse a `long long` from a string.
*
* This function makes fewer guarantees than the C runtime `strtoll`:
*
@@ -1793,9 +1795,6 @@ extern SDL_DECLSPEC unsigned long SDLCALL SDL_strtoul(const char *str, char **en
* - It is unspecified what this function returns when the parsed integer does
* not fit inside a `long long`.
*
- * Also note that unlike the C runtime `strtoll`, this function returns an
- * Sint64, not a `long long`.
- *
* \param str The null-terminated string to read. Must not be NULL.
* \param endp If not NULL, the address of the first invalid character (i.e.
* the next character after the parsed number) will be written to
@@ -1803,7 +1802,7 @@ extern SDL_DECLSPEC unsigned long SDLCALL SDL_strtoul(const char *str, char **en
* \param base The base of the integer to read. The values 0, 10 and 16 are
* supported. If 0, the base will be inferred from the integer's
* prefix.
- * \returns The parsed Sint64.
+ * \returns The parsed `long long`.
*
* \threadsafety It is safe to call this function from any thread.
*
@@ -1817,10 +1816,10 @@ extern SDL_DECLSPEC unsigned long SDLCALL SDL_strtoul(const char *str, char **en
* \sa SDL_strtod
* \sa SDL_lltoa
*/
-extern SDL_DECLSPEC Sint64 SDLCALL SDL_strtoll(const char *str, char **endp, int base);
+extern SDL_DECLSPEC long long SDLCALL SDL_strtoll(const char *str, char **endp, int base);
/**
- * Parse a Uint64 from a string.
+ * Parse an `unsigned long long` from a string.
*
* This function makes fewer guarantees than the C runtime `strtoull`:
*
@@ -1829,9 +1828,6 @@ extern SDL_DECLSPEC Sint64 SDLCALL SDL_strtoll(const char *str, char **endp, int
* - It is unspecified what this function returns when the parsed integer does
* not fit inside a `long long`.
*
- * Also note that unlike the C runtime `strtoull`, this function returns a
- * Uint64, not an `unsigned long long`.
- *
* \param str The null-terminated string to read. Must not be NULL.
* \param endp If not NULL, the address of the first invalid character (i.e.
* the next character after the parsed number) will be written to
@@ -1839,7 +1835,7 @@ extern SDL_DECLSPEC Sint64 SDLCALL SDL_strtoll(const char *str, char **endp, int
* \param base The base of the integer to read. The values 0, 10 and 16 are
* supported. If 0, the base will be inferred from the integer's
* prefix.
- * \returns The parsed Uint64.
+ * \returns The parsed `unsigned long long`.
*
* \threadsafety It is safe to call this function from any thread.
*
@@ -1853,7 +1849,7 @@ extern SDL_DECLSPEC Sint64 SDLCALL SDL_strtoll(const char *str, char **endp, int
* \sa SDL_strtod
* \sa SDL_ulltoa
*/
-extern SDL_DECLSPEC Uint64 SDLCALL SDL_strtoull(const char *str, char **endp, int base);
+extern SDL_DECLSPEC unsigned long long SDLCALL SDL_strtoull(const char *str, char **endp, int base);
/**
* Parse a `double` from a string.
diff --git a/src/SDL_properties.c b/src/SDL_properties.c
index 78997d7ddf631..2a4f2aca83982 100644
--- a/src/SDL_properties.c
+++ b/src/SDL_properties.c
@@ -617,7 +617,7 @@ Sint64 SDL_GetNumberProperty(SDL_PropertiesID props, const char *name, Sint64 de
if (SDL_FindInHashTable(properties->props, name, (const void **)&property)) {
switch (property->type) {
case SDL_PROPERTY_TYPE_STRING:
- value = SDL_strtoll(property->value.string_value, NULL, 0);
+ value = (Sint64)SDL_strtoll(property->value.string_value, NULL, 0);
break;
case SDL_PROPERTY_TYPE_NUMBER:
value = property->value.number_value;
diff --git a/src/dynapi/SDL_dynapi_procs.h b/src/dynapi/SDL_dynapi_procs.h
index b9d30ea46e898..3a60c9f34c806 100644
--- a/src/dynapi/SDL_dynapi_procs.h
+++ b/src/dynapi/SDL_dynapi_procs.h
@@ -1083,7 +1083,7 @@ SDL_DYNAPI_PROC(int,SDL_isspace,(int a),(a),return)
SDL_DYNAPI_PROC(int,SDL_isupper,(int a),(a),return)
SDL_DYNAPI_PROC(int,SDL_isxdigit,(int a),(a),return)
SDL_DYNAPI_PROC(char*,SDL_itoa,(int a, char *b, int c),(a,b,c),return)
-SDL_DYNAPI_PROC(char*,SDL_lltoa,(Sint64 a, char *b, int c),(a,b,c),return)
+SDL_DYNAPI_PROC(char*,SDL_lltoa,(long long a, char *b, int c),(a,b,c),return)
SDL_DYNAPI_PROC(double,SDL_log,(double a),(a),return)
SDL_DYNAPI_PROC(double,SDL_log10,(double a),(a),return)
SDL_DYNAPI_PROC(float,SDL_log10f,(float a),(a),return)
@@ -1141,9 +1141,9 @@ SDL_DYNAPI_PROC(char*,SDL_strstr,(const char *a, const char *b),(a,b),return)
SDL_DYNAPI_PROC(double,SDL_strtod,(const char *a, char **b),(a,b),return)
SDL_DYNAPI_PROC(char*,SDL_strtok_r,(char *a, const char *b, char **c),(a,b,c),return)
SDL_DYNAPI_PROC(long,SDL_strtol,(const char *a, char **b, int c),(a,b,c),return)
-SDL_DYNAPI_PROC(Sint64,SDL_strtoll,(const char *a, char **b, int c),(a,b,c),return)
+SDL_DYNAPI_PROC(long long,SDL_strtoll,(const char *a, char **b, int c),(a,b,c),return)
SDL_DYNAPI_PROC(unsigned long,SDL_strtoul,(const char *a, char **b, int c),(a,b,c),return)
-SDL_DYNAPI_PROC(Uint64,SDL_strtoull,(const char *a, char **b, int c),(a,b,c),return)
+SDL_DYNAPI_PROC(unsigned long long,SDL_strtoull,(const char *a, char **b, int c),(a,b,c),return)
SDL_DYNAPI_PROC(char*,SDL_strupr,(char *a),(a),return)
SDL_DYNAPI_PROC(double,SDL_tan,(double a),(a),return)
SDL_DYNAPI_PROC(float,SDL_tanf,(float a),(a),return)
@@ -1152,7 +1152,7 @@ SDL_DYNAPI_PROC(int,SDL_toupper,(int a),(a),return)
SDL_DYNAPI_PROC(double,SDL_trunc,(double a),(a),return)
SDL_DYNAPI_PROC(float,SDL_truncf,(float a),(a),return)
SDL_DYNAPI_PROC(char*,SDL_uitoa,(unsigned int a, char *b, int c),(a,b,c),return)
-SDL_DYNAPI_PROC(char*,SDL_ulltoa,(Uint64 a, char *b, int c),(a,b,c),return)
+SDL_DYNAPI_PROC(char*,SDL_ulltoa,(unsigned long long a, char *b, int c),(a,b,c),return)
SDL_DYNAPI_PROC(char*,SDL_ultoa,(unsigned long a, char *b, int c),(a,b,c),return)
SDL_DYNAPI_PROC(int,SDL_unsetenv,(const char *a),(a),return)
SDL_DYNAPI_PROC(size_t,SDL_utf8strlcpy,(SDL_OUT_Z_CAP(c) char *a, const char *b, size_t c),(a,b,c),return)
diff --git a/src/joystick/SDL_steam_virtual_gamepad.c b/src/joystick/SDL_steam_virtual_gamepad.c
index 7df021c553b2c..1250dd6972d02 100644
--- a/src/joystick/SDL_steam_virtual_gamepad.c
+++ b/src/joystick/SDL_steam_virtual_gamepad.c
@@ -211,7 +211,7 @@ bool SDL_UpdateSteamVirtualGamepadInfo(void)
} else if (SDL_strcmp(line, "type") == 0) {
info.type = SDL_GetGamepadTypeFromString(value);
} else if (SDL_strcmp(line, "handle") == 0) {
- info.handle = SDL_strtoull(value, NULL, 0);
+ info.handle = (Uint64)SDL_strtoull(value, NULL, 0);
}
}
}
diff --git a/src/stdlib/SDL_string.c b/src/stdlib/SDL_string.c
index bd3d8a9f1dea1..907f9537c0eaa 100644
--- a/src/stdlib/SDL_string.c
+++ b/src/stdlib/SDL_string.c
@@ -485,10 +485,10 @@ static size_t SDL_ScanUintPtrT(const char *text, int radix, uintptr_t *valuep)
#endif
#if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOLL) || !defined(HAVE_STRTOULL)
-static size_t SDL_ScanLongLong(const char *text, int count, int radix, Sint64 *valuep)
+static size_t SDL_ScanLongLong(const char *text, int count, int radix, long long *valuep)
{
const char *textstart = text;
- Sint64 value = 0;
+ long long value = 0;
bool negative = false;
if (*text == '-') {
@@ -529,13 +529,13 @@ static size_t SDL_ScanLongLong(const char *text, int count, int radix, Sint64 *v
#endif
#if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOULL)
-static size_t SDL_ScanUnsignedLongLong(const char *text, int count, int radix, Uint64 *valuep)
+static size_t SDL_ScanUnsignedLongLong(const char *text, int count, int radix, unsigned long long *valuep)
{
const char *textstart = text;
- Uint64 value = 0;
+ unsigned long long value = 0;
if (*text == '-') {
- return SDL_ScanLongLong(text, count, radix, (Sint64 *)valuep);
+ return SDL_ScanLongLong(text, count, radix, (long long *)valuep);
}
if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
@@ -592,7 +592,7 @@ static size_t SDL_ScanFloat(const char *text, double *valuep)
}
}
if (valuep && text > textstart) {
- if (negative && value) {
+ if (negative && value != 0.0) {
*valuep = -value;
} else {
*valuep = value;
@@ -1133,7 +1133,7 @@ char *SDL_ultoa(unsigned long value, char *string, int radix)
#endif // HAVE__ULTOA
}
-char *SDL_lltoa(Sint64 value, char *string, int radix)
+char *SDL_lltoa(long long value, char *string, int radix)
{
#ifdef HAVE__I64TOA
return _i64toa(value, string, radix);
@@ -1151,7 +1151,7 @@ char *SDL_lltoa(Sint64 value, char *string, int radix)
#endif // HAVE__I64TOA
}
-char *SDL_ulltoa(Uint64 value, char *string, int radix)
+char *SDL_ulltoa(unsigned long long value, char *string, int radix)
{
#ifdef HAVE__UI64TOA
return _ui64toa(value, string, radix);
@@ -1217,8 +1217,7 @@ long SDL_strtol(const char *string, char **endp, int base)
#endif // HAVE_STRTOL
}
-unsigned long
-SDL_strtoul(const char *string, char **endp, int base)
+unsigned long SDL_strtoul(const char *string, char **endp, int base)
{
#ifdef HAVE_STRTOUL
return strtoul(string, endp, base);
@@ -1242,16 +1241,16 @@ SDL_strtoul(const char *string, char **endp, int base)
#endif // HAVE_STRTOUL
}
-Sint64 SDL_strtoll(const char *string, char **endp, int base)
+long long SDL_strtoll(const char *string, char **endp, int base)
{
#ifdef HAVE_STRTOLL
return strtoll(string, endp, base);
#else
size_t len;
- Sint64 value = 0;
+ long long value = 0;
if (!base) {
- if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
+ if ((SDL_strlen(string) > 2) && (SDL_strncasecmp(string, "0x", 2) == 0)) {
base = 16;
} else {
base = 10;
@@ -1266,16 +1265,16 @@ Sint64 SDL_strtoll(const char *string, char **endp, int base)
#endif // HAVE_STRTOLL
}
-Uint64 SDL_strtoull(const char *string, char **endp, int base)
+unsigned long long SDL_strtoull(const char *string, char **endp, int base)
{
#ifdef HAVE_STRTOULL
return strtoull(string, endp, base);
#else
size_t len;
- Uint64 value = 0;
+ unsigned long long value = 0;
if (!base) {
- if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
+ if ((SDL_strlen(string) > 2) && (SDL_strncasecmp(string, "0x", 2) == 0)) {
base = 16;
} else {
base = 10;
@@ -1519,7 +1518,7 @@ int SDL_vsscanf(const char *text, SDL_SCANF_FORMAT_STRING const char *fmt, va_li
SDL_FALLTHROUGH;
case 'd':
if (inttype == DO_LONGLONG) {
- Sint64 value = 0;
+ long long value = 0;
advance = SDL_ScanLongLong(text, count, radix, &value);
text += advance;
if (advance && !suppress) {
@@ -1528,7 +1527,7 @@ int SDL_vsscanf(const char *text, SDL_SCANF_FORMAT_STRING const char *fmt, va_li
++result;
}
} else if (inttype == DO_SIZE_T) {
- Sint64 value = 0;
+ long long value = 0;
advance = SDL_ScanLongLong(text, count, radix, &value);
text += advance;
if (advance && !suppress) {
@@ -1580,7 +1579,7 @@ int SDL_vsscanf(const char *text, SDL_SCANF_FORMAT_STRING const char *fmt, va_li
SDL_FALLTHROUGH;
case 'u':
if (inttype == DO_LONGLONG) {
- Uint64 value = 0;
+ unsigned long long value = 0;
advance = SDL_ScanUnsignedLongLong(text, count, radix, &value);
text += advance;
if (advance && !suppress) {
@@ -1589,7 +1588,7 @@ int SDL_vsscanf(const char *text, SDL_SCANF_FORMAT_STRING const char *fmt, va_li
++result;
}
} else if (inttype == DO_SIZE_T) {
- Uint64 value = 0;
+ unsigned long long value = 0;
advance = SDL_ScanUnsignedLongLong(text, count, radix, &value);
text += advance;
if (advance && !suppress) {
@@ -1938,7 +1937,7 @@ static size_t SDL_PrintUnsignedLong(char *text, size_t maxlen, SDL_FormatInfo *i
return SDL_PrintString(text, maxlen, info, num);
}
-static size_t SDL_PrintLongLong(char *text, size_t maxlen, SDL_FormatInfo *info, Sint64 value)
+static size_t SDL_PrintLongLong(char *text, size_t maxlen, SDL_FormatInfo *info, long long value)
{
char num[130], *p = num;
@@ -1951,7 +1950,7 @@ static size_t SDL_PrintLongLong(char *text, size_t maxlen, SDL_FormatInfo *info,
return SDL_PrintString(text, maxlen, info, num);
}
-static size_t SDL_PrintUnsignedLongLong(char *text, size_t maxlen, SDL_FormatInfo *info, Uint64 value)
+static size_t SDL_PrintUnsignedLongLong(char *text, size_t maxlen, SDL_FormatInfo *info, unsigned long long value)
{
char num[130];
@@ -1968,7 +1967,7 @@ static size_t SDL_PrintFloat(char *text, size_t maxlen, SDL_FormatInfo *info, do
int precision = info->precision;
// This isn't especially accurate, but hey, it's easy. :)
- Uint64 value;
+ unsigned long long value;
if (arg < 0) {
num[length++] = '-';
@@ -1976,7 +1975,7 @@ static size_t SDL_PrintFloat(char *text, size_t maxlen, SDL_FormatInfo *info, do
} else if (info->force_sign) {
num[length++] = '+';
}
- value = (Uint64)arg;
+ value = (unsigned long long)arg;
integer_length = SDL_PrintUnsignedLongLong(&num[length], sizeof(num) - length, NULL, value);
length += integer_length;
arg -= value;
@@ -2203,7 +2202,7 @@ int SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, SDL_PRINTF_FO
break;
case DO_LONGLONG:
length += SDL_PrintLongLong(TEXT_AND_LEN_ARGS, &info,
- va_arg(ap, Sint64));
+ va_arg(ap, long long));
break;
case DO_SIZE_T:
length += SDL_PrintLongLong(TEXT_AND_LEN_ARGS, &info,
@@ -2241,8 +2240,7 @@ int SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, SDL_PRINTF_FO
switch (inttype) {
case DO_INT:
length += SDL_PrintUnsignedLong(TEXT_AND_LEN_ARGS, &info,
- (unsigned long)
- va_arg(ap, unsigned int));
+ va_arg(ap, unsigned int));
break;
case DO_LONG:
length += SDL_PrintUnsignedLong(TEXT_AND_LEN_ARGS, &info,
@@ -2250,7 +2248,7 @@ int SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, SDL_PRINTF_FO
break;
case DO_LONGLONG:
length += SDL_PrintUnsignedLongLong(TEXT_AND_LEN_ARGS, &info,
- va_arg(ap, Uint64));
+ va_arg(ap, unsigned long long));
break;
case DO_SIZE_T:
length += SDL_PrintUnsignedLongLong(TEXT_AND_LEN_ARGS, &info,
diff --git a/test/testqsort.c b/test/testqsort.c
index caa4a05f2ef5b..bf20c4abe33dd 100644
--- a/test/testqsort.c
+++ b/test/testqsort.c
@@ -86,7 +86,7 @@ int main(int argc, char *argv[])
if (!seed_seen) {
char *endptr = NULL;
- seed = SDL_strtoull(argv[i], &endptr, 0);
+ seed = (Uint64)SDL_strtoull(argv[i], &endptr, 0);
if (endptr != argv[i] && *endptr == '\0') {
seed_seen = 1;
consumed = 1;