Skip to content

gh-110819: Fix ‘kind’ may be used uninitialized warning in longobject #116599

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

Merged
merged 3 commits into from
Mar 12, 2024

Conversation

sobolevn
Copy link
Member

@sobolevn sobolevn commented Mar 11, 2024

I've also added an assert to make sure that both parameters writer and bytes_writer cannot be passed at the same time.

@erlend-aasland
Copy link
Contributor

@sobolevn
Copy link
Member Author

sobolevn commented Mar 11, 2024

Sorry, I've missed it! Differences:

  • I'm using 0 as default, @FFY00 uses -1, both work in quite the same way
  • I added assert for writer and bytes_writer params, @FFY00 added an assert for kind var

I am happy to close my PR and help @FFY00 finish the first PR if that works :)

@sobolevn sobolevn changed the title gh-116592: Fix ‘kind’ may be used uninitialized warning in longobject gh-110819: Fix ‘kind’ may be used uninitialized warning in longobject Mar 11, 2024
@erlend-aasland
Copy link
Contributor

This is a larger diff, but it avoids the initialisation:

diff --git a/Objects/longobject.c b/Objects/longobject.c
index 2d1c6ad788..a631dc05b2 100644
--- a/Objects/longobject.c
+++ b/Objects/longobject.c
@@ -2197 +2196,0 @@ long_format_binary(PyObject *aa, int base, int alternate,
-    int kind;
@@ -2249 +2247,0 @@ long_format_binary(PyObject *aa, int base, int alternate,
-        kind = writer->kind;
@@ -2260 +2257,0 @@ long_format_binary(PyObject *aa, int base, int alternate,
-        kind = PyUnicode_KIND(v);
@@ -2321,8 +2317,0 @@ long_format_binary(PyObject *aa, int base, int alternate,
-    else if (kind == PyUnicode_1BYTE_KIND) {
-        Py_UCS1 *p;
-        WRITE_UNICODE_DIGITS(Py_UCS1);
-    }
-    else if (kind == PyUnicode_2BYTE_KIND) {
-        Py_UCS2 *p;
-        WRITE_UNICODE_DIGITS(Py_UCS2);
-    }
@@ -2330,3 +2319,14 @@ long_format_binary(PyObject *aa, int base, int alternate,
-        Py_UCS4 *p;
-        assert (kind == PyUnicode_4BYTE_KIND);
-        WRITE_UNICODE_DIGITS(Py_UCS4);
+        int kind = writer ? writer->kind : PyUnicode_KIND(v);
+        if (kind == PyUnicode_1BYTE_KIND) {
+            Py_UCS1 *p;
+            WRITE_UNICODE_DIGITS(Py_UCS1);
+        }
+        else if (kind == PyUnicode_2BYTE_KIND) {
+            Py_UCS2 *p;
+            WRITE_UNICODE_DIGITS(Py_UCS2);
+        }
+        else {
+            Py_UCS4 *p;
+            assert (kind == PyUnicode_4BYTE_KIND);
+            WRITE_UNICODE_DIGITS(Py_UCS4);
+        }

@FFY00
Copy link
Member

FFY00 commented Mar 11, 2024

I am happy to close my PR and help @FFY00 finish the first PR if that works :)

I don't have much bandwidth right now, we can close mine in favor of this PR.

@sobolevn
Copy link
Member Author

@erlend-aasland yes, I liked your idea. I've implemented the change. Thank you!
@FFY00 thanks for working on this ❤️

Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@sobolevn sobolevn merged commit eb947cd into python:main Mar 12, 2024
@miss-islington-app

This comment was marked as outdated.

@sobolevn
Copy link
Member Author

Thanks everyone involved 👍

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Mar 12, 2024
…gobject` (pythonGH-116599)

(cherry picked from commit eb947cd)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
@miss-islington-app

This comment was marked as outdated.

@bedevere-app
Copy link

bedevere-app bot commented Mar 12, 2024

GH-116648 is a backport of this pull request to the 3.12 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.12 only security fixes label Mar 12, 2024
@sobolevn

This comment was marked as outdated.

sobolevn added a commit to sobolevn/cpython that referenced this pull request Mar 12, 2024
…in `longobject` (pythonGH-116599)

(cherry picked from commit eb947cd)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
@bedevere-app
Copy link

bedevere-app bot commented Mar 12, 2024

GH-116650 is a backport of this pull request to the 3.11 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.11 only security fixes label Mar 12, 2024
sobolevn added a commit to sobolevn/cpython that referenced this pull request Mar 12, 2024
…in `longobject` (pythonGH-116599)

(cherry picked from commit eb947cd)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
sobolevn added a commit that referenced this pull request Mar 12, 2024
…ngobject` (GH-116599) (#116648)

gh-110819: Fix ‘kind’ may be used uninitialized warning in `longobject` (GH-116599)
(cherry picked from commit eb947cd)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
sobolevn added a commit that referenced this pull request Mar 12, 2024
adorilson pushed a commit to adorilson/cpython that referenced this pull request Mar 25, 2024
diegorusso pushed a commit to diegorusso/cpython that referenced this pull request Apr 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants