Skip to content

Commit ec3959e

Browse files
WillAydmliu08
authored andcommitted
Removed GNU strdup extension from JSON (pandas-dev#49780)
* removed GNU extension from JSON * Revert "removed GNU extension from JSON" This reverts commit cd6b630. * try again * Better setlocale error * Copied string * make cpplint happy * typo fix * memcpy instead of snprintf * try catchsegv * single doc build * Revert "single doc build" This reverts commit c9f5225. * Revert "try catchsegv" This reverts commit a6199a8. * Fixed wrong free
1 parent 997392b commit ec3959e

File tree

4 files changed

+25
-18
lines changed

4 files changed

+25
-18
lines changed

pandas/_libs/src/headers/portable.h

-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
#ifndef _PANDAS_PORTABLE_H_
22
#define _PANDAS_PORTABLE_H_
33

4-
// To get `strdup` from strings.h
5-
#ifndef _XOPEN_SOURCE
6-
#define _XOPEN_SOURCE 600
7-
#endif
8-
94
#include <string.h>
105

116
#if defined(_MSC_VER)
127
#define strcasecmp( s1, s2 ) _stricmp( s1, s2 )
13-
#define strdup _strdup
148
#endif
159

1610
// GH-23516 - works around locale perf issues

pandas/_libs/src/ujson/lib/ultrajsondec.c

+11-5
Original file line numberDiff line numberDiff line change
@@ -1174,15 +1174,21 @@ JSOBJ JSON_DecodeObject(JSONObjectDecoder *dec, const char *buffer,
11741174
ds.dec = dec;
11751175

11761176
locale = setlocale(LC_NUMERIC, NULL);
1177+
if (!locale) {
1178+
return SetError(&ds, -1, "setlocale call failed");
1179+
}
1180+
11771181
if (strcmp(locale, "C")) {
1178-
locale = strdup(locale);
1179-
if (!locale) {
1180-
return SetError(&ds, -1, "Could not reserve memory block");
1182+
size_t len = strlen(locale) + 1;
1183+
char *saved_locale = malloc(len);
1184+
if (saved_locale == NULL) {
1185+
return SetError(&ds, -1, "Could not reserve memory block");
11811186
}
1187+
memcpy(saved_locale, locale, len);
11821188
setlocale(LC_NUMERIC, "C");
11831189
ret = decode_any(&ds);
1184-
setlocale(LC_NUMERIC, locale);
1185-
free(locale);
1190+
setlocale(LC_NUMERIC, saved_locale);
1191+
free(saved_locale);
11861192
} else {
11871193
ret = decode_any(&ds);
11881194
}

pandas/_libs/src/ujson/lib/ultrajsonenc.c

+13-6
Original file line numberDiff line numberDiff line change
@@ -1176,16 +1176,23 @@ char *JSON_EncodeObject(JSOBJ obj, JSONObjectEncoder *enc, char *_buffer,
11761176
enc->offset = enc->start;
11771177

11781178
locale = setlocale(LC_NUMERIC, NULL);
1179+
if (!locale) {
1180+
SetError(NULL, enc, "setlocale call failed");
1181+
return NULL;
1182+
}
1183+
11791184
if (strcmp(locale, "C")) {
1180-
locale = strdup(locale);
1181-
if (!locale) {
1182-
SetError(NULL, enc, "Could not reserve memory block");
1183-
return NULL;
1185+
size_t len = strlen(locale) + 1;
1186+
char *saved_locale = malloc(len);
1187+
if (saved_locale == NULL) {
1188+
SetError(NULL, enc, "Could not reserve memory block");
1189+
return NULL;
11841190
}
1191+
memcpy(saved_locale, locale, len);
11851192
setlocale(LC_NUMERIC, "C");
11861193
encode(obj, enc, NULL, 0);
1187-
setlocale(LC_NUMERIC, locale);
1188-
free(locale);
1194+
setlocale(LC_NUMERIC, saved_locale);
1195+
free(saved_locale);
11891196
} else {
11901197
encode(obj, enc, NULL, 0);
11911198
}

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ def srcpath(name=None, suffix=".pyx", subdir="src"):
649649
"pandas/_libs/src/datetime",
650650
numpy.get_include(),
651651
],
652-
extra_compile_args=(["-D_GNU_SOURCE"] + extra_compile_args),
652+
extra_compile_args=(extra_compile_args),
653653
extra_link_args=extra_link_args,
654654
define_macros=macros,
655655
)

0 commit comments

Comments
 (0)