Skip to content

Commit

Permalink
Fix build warning for use of strdup in ultrajson
Browse files Browse the repository at this point in the history
When building with `-std=c99` on Linux this gives a warning:

```
[2/3] Compiling C object pandas/_libs/json.cpython-310-x86_64-linux-gnu.so.p/src_ujson_lib_ultrajsonenc.c.o
../pandas/_libs/src/ujson/lib/ultrajsonenc.c: In function 'JSON_EncodeObject':
../pandas/_libs/src/ujson/lib/ultrajsonenc.c:1180:18: warning: implicit declaration of function 'strdup'; did you mean 'strcmp'? [-Wimplicit-function-declaration]
 1180 |         locale = strdup(locale);
      |                  ^~~~~~
      |                  strcmp
../pandas/_libs/src/ujson/lib/ultrajsonenc.c:1180:16: warning: assignment to 'char *' from 'int' makes pointer from integer without a cast [-Wint-conversion]
 1180 |         locale = strdup(locale);
```

And for the MSVC build in CI the warnings look like:
```
building 'pandas._libs.json' extension
creating build\temp.win-amd64-cpython-310\Release\pandas\_libs\src\ujson
creating build\temp.win-amd64-cpython-310\Release\pandas\_libs\src\ujson\lib
creating build\temp.win-amd64-cpython-310\Release\pandas\_libs\src\ujson\python
"C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.33.31629\bin\HostX86\x64\cl.exe" /c /nologo /O2 /W3 /GL /DNDEBUG /MD -DNPY_NO_DEPRECATED_API=0 -Ipandas/_libs/src/ujson/python -Ipandas/_libs/src/ujson/lib -Ipandas/_libs/src/datetime -IC:\Users\runneradmin\micromamba\envs\test\lib\site-packages\numpy\core\include -IC:\Users\runneradmin\micromamba\envs\test\include -IC:\Users\runneradmin\micromamba\envs\test\Include "-IC:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.33.31629\include" "-IC:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.33.31629\ATLMFC\include" "-IC:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\VS\include" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\um" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\shared" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\winrt" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\cppwinrt" "-IC:\Program Files (x86)\Windows Kits\NETFXSDK\4.8\include\um" /Tcpandas/_libs/src/ujson/lib/ultrajsondec.c /Fobuild\temp.win-amd64-cpython-310\Release\pandas/_libs/src/ujson/lib/ultrajsondec.obj -D_GNU_SOURCE
ultrajsondec.c
pandas/_libs/src/ujson/lib/ultrajsondec.c(1178): warning C4996: 'strdup': The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conformant name: _strdup. See online help for details.
```

I considered avoiding the code duplication by putting this block in a
shared file, but the two files touched here already have quite a bit
of duplicated code, so that's best left alone.
  • Loading branch information
rgommers committed Sep 2, 2022
1 parent 298dac3 commit 21dbc27
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pandas/_libs/src/ujson/lib/ultrajsondec.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ Numeric decoder derived from TCL library
#define NULL 0
#endif

/* `strdup` is POSIX not C99, and MSVC calls it `_strdup` */
#ifndef strdup
#ifdef _MSC_VER
#define strdup _strdup
#else
extern char* strdup(const char*);
#endif
#endif

struct DecoderState {
char *start;
char *end;
Expand Down
9 changes: 9 additions & 0 deletions pandas/_libs/src/ujson/lib/ultrajsonenc.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ Numeric decoder derived from TCL library
#define FALSE 0
#endif

/* `strdup` is POSIX not C99, and MSVC calls it `_strdup` */
#ifndef strdup
#ifdef _MSC_VER
#define strdup _strdup
#else
extern char* strdup(const char*);
#endif
#endif

/*
Worst cases being:
Expand Down

0 comments on commit 21dbc27

Please sign in to comment.