Skip to content

Compiler warnings on string comparisons in _testcapi #109469

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
iritkatriel opened this issue Sep 15, 2023 · 5 comments · Fixed by #109558
Closed

Compiler warnings on string comparisons in _testcapi #109469

iritkatriel opened this issue Sep 15, 2023 · 5 comments · Fixed by #109558
Labels
build The build process and cross-build extension-modules C modules in the Modules dir tests Tests in the Lib/test dir

Comments

@iritkatriel
Copy link
Member

iritkatriel commented Sep 15, 2023

./Modules/_testcapimodule.c:226:18: warning: result of comparison against a string literal is unspecified (use an explicit string comparison function instead) [-Wstring-compare]
        assert(v != UNINITIALIZED_PTR);
                 ^  ~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/assert.h:99:25: note: expanded from macro 'assert'
    (__builtin_expect(!(e), 0) ? __assert_rtn(__func__, __ASSERT_FILE_NAME, __LINE__, #e) : (void)0)
                        ^
./Modules/_testcapimodule.c:238:14: warning: result of comparison against a string literal is unspecified (use an explicit string comparison function instead) [-Wstring-compare]
    assert(k == UNINITIALIZED_PTR);
             ^  ~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/assert.h:99:25: note: expanded from macro 'assert'
    (__builtin_expect(!(e), 0) ? __assert_rtn(__func__, __ASSERT_FILE_NAME, __LINE__, #e) : (void)0)
                        ^
./Modules/_testcapimodule.c:239:14: warning: result of comparison against a string literal is unspecified (use an explicit string comparison function instead) [-Wstring-compare]
    assert(v == UNINITIALIZED_PTR);
             ^  ~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/assert.h:99:25: note: expanded from macro 'assert'
    (__builtin_expect(!(e), 0) ? __assert_rtn(__func__, __ASSERT_FILE_NAME, __LINE__, #e) : (void)0)
                        ^
4 warnings generated.
./Modules/_testcapi/dict.c:289:16: warning: result of comparison against a string literal is unspecified (use an explicit string comparison function instead) [-Wstring-compare]
    assert(key == UNINITIALIZED_PTR);
               ^  ~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/assert.h:99:25: note: expanded from macro 'assert'
    (__builtin_expect(!(e), 0) ? __assert_rtn(__func__, __ASSERT_FILE_NAME, __LINE__, #e) : (void)0)
                        ^
./Modules/_testcapi/dict.c:290:18: warning: result of comparison against a string literal is unspecified (use an explicit string comparison function instead) [-Wstring-compare]
    assert(value == UNINITIALIZED_PTR);
                 ^  ~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/assert.h:99:25: note: expanded from macro 'assert'
    (__builtin_expect(!(e), 0) ? __assert_rtn(__func__, __ASSERT_FILE_NAME, __LINE__, #e) : (void)0)
                        ^
2 warnings generated.
./Modules/_testcapi/exceptions.c:129:17: warning: result of comparison against a string literal is unspecified (use an explicit string comparison function instead) [-Wstring-compare]
    assert(type != UNINITIALIZED_PTR);
                ^  ~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/assert.h:99:25: note: expanded from macro 'assert'
    (__builtin_expect(!(e), 0) ? __assert_rtn(__func__, __ASSERT_FILE_NAME, __LINE__, #e) : (void)0)
                        ^
./Modules/_testcapi/exceptions.c:130:18: warning: result of comparison against a string literal is unspecified (use an explicit string comparison function instead) [-Wstring-compare]
    assert(value != UNINITIALIZED_PTR);
                 ^  ~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/assert.h:99:25: note: expanded from macro 'assert'
    (__builtin_expect(!(e), 0) ? __assert_rtn(__func__, __ASSERT_FILE_NAME, __LINE__, #e) : (void)0)
                        ^
./Modules/_testcapi/exceptions.c:131:15: warning: result of comparison against a string literal is unspecified (use an explicit string comparison function instead) [-Wstring-compare]
    assert(tb != UNINITIALIZED_PTR);
              ^  ~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/assert.h:99:25: note: expanded from macro 'assert'
    (__builtin_expect(!(e), 0) ? __assert_rtn(__func__, __ASSERT_FILE_NAME, __LINE__, #e) : (void)0)

Linked PRs

@iritkatriel iritkatriel added build The build process and cross-build tests Tests in the Lib/test dir extension-modules C modules in the Modules dir labels Sep 15, 2023
@sunmy2019
Copy link
Member

sunmy2019 commented Sep 16, 2023

Changing UNINITIALIZED_PTR to an invalid address literal should resolve this.

such as #define UNINITIALIZED_PTR 0xcdcdcdcdcdcdcdcd on x64.

@erlend-aasland
Copy link
Contributor

cc. @serhiy-storchaka

@serhiy-storchaka
Copy link
Member

The point was to use valid address. On some platforms, not all bit combinations can be interpreted as address, and interpreting them as address can even lead to crash.

Does the following change help with warning?

-#define UNINITIALIZED_PTR ((void *)"uninitialized")
+static const char uninitialized[] = "uninitialized";
+#define UNINITIALIZED_PTR ((void *)uninitialized)

@iritkatriel
Copy link
Member Author

Does the following change help with warning?

-#define UNINITIALIZED_PTR ((void *)"uninitialized")
+static const char uninitialized[] = "uninitialized";
+#define UNINITIALIZED_PTR ((void *)uninitialized)

Yes.

serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Sep 18, 2023
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Sep 19, 2023
…testcapi (pythonGH-109533)

(cherry picked from commit ed582a2)

Co-authored-by: Serhiy Storchaka <[email protected]>
csm10495 pushed a commit to csm10495/cpython that referenced this issue Sep 28, 2023
Yhg1s pushed a commit that referenced this issue Oct 2, 2023
…_testcapi (GH-109533) (#109558)

gh-109469: Silence compiler warnings on string comparisons in _testcapi (GH-109533)
(cherry picked from commit ed582a2)

Co-authored-by: Serhiy Storchaka <[email protected]>
@hugovk
Copy link
Member

hugovk commented Nov 9, 2023

Thanks for the fixes!

@hugovk hugovk closed this as completed Nov 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
build The build process and cross-build extension-modules C modules in the Modules dir tests Tests in the Lib/test dir
Projects
None yet
5 participants