Skip to content

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

Merged
sobolevn merged 3 commits into
python:mainfrom
sobolevn:issue-116592
Mar 12, 2024
Merged

gh-110819: Fix ‘kind’ may be used uninitialized warning in longobject#116599
sobolevn merged 3 commits into
python:mainfrom
sobolevn:issue-116592

Conversation

@sobolevn

@sobolevn sobolevn commented Mar 11, 2024

Copy link
Copy Markdown
Member

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
Copy Markdown
Contributor

@sobolevn

sobolevn commented Mar 11, 2024

Copy link
Copy Markdown
Member Author

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
Comment thread Objects/longobject.c
@erlend-aasland

Copy link
Copy Markdown
Contributor

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

Details
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

FFY00 commented Mar 11, 2024

Copy link
Copy Markdown
Member

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
Copy Markdown
Member Author

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

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

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
Copy Markdown
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

bedevere-app Bot commented Mar 12, 2024

Copy link
Copy Markdown

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

bedevere-app Bot commented Mar 12, 2024

Copy link
Copy Markdown

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