FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

GCC conversion warnings from internal header files · Issue #106550 · python/cpython · GitHub

Repository navigation

GCC conversion warnings from internal header files #106550

Closed

Description

Bug report

Compiling Cython-generated code with GCC -Wconversion produces a bunch of warnings within inline function defined in internal CPython header files.

/usr/include/python3.12/internal/pycore_code.h: In function ‘write_varint’:
/usr/include/python3.12/internal/pycore_code.h:362:12: warning: conversion from ‘unsigned int’ to ‘uint8_t’ {aka ‘unsigned char’} may change value [-Wconversion]
  362 |     *ptr = val;
      |            ^~~
/usr/include/python3.12/internal/pycore_code.h: In function ‘write_signed_varint’:
/usr/include/python3.12/internal/pycore_code.h:375:30: warning: conversion to ‘unsigned int’ from ‘int’ may change the sign of the result [-Wsign-conversion]
  375 |     return write_varint(ptr, val);
      |                              ^~~
/usr/include/python3.12/internal/pycore_code.h: In function ‘write_location_entry_start’:
/usr/include/python3.12/internal/pycore_code.h:382:12: warning: conversion from ‘int’ to ‘uint8_t’ {aka ‘unsigned char’} may change value [-Wconversion]
  382 |     *ptr = 128 | (code << 3) | (length - 1);
      |            ^~~
/usr/include/python3.12/internal/pycore_code.h: In function ‘adaptive_counter_bits’:
/usr/include/python3.12/internal/pycore_code.h:423:45: warning: conversion from ‘int’ to ‘uint16_t’ {aka ‘short unsigned int’} may change value [-Wconversion]
  423 |     return (value << ADAPTIVE_BACKOFF_BITS) |
      |            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
  424 |         (backoff & ((1<<ADAPTIVE_BACKOFF_BITS)-1));
      |         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/python3.12/internal/pycore_code.h: In function ‘adaptive_counter_backoff’:
/usr/include/python3.12/internal/pycore_code.h:446:26: warning: conversion to ‘unsigned int’ from ‘int’ may change the sign of the result [-Wsign-conversion]
  446 |     unsigned int value = (1 << backoff) - 1;
      |                          ^
/usr/include/python3.12/internal/pycore_code.h:447:34: warning: conversion to ‘int’ from ‘unsigned int’ may change the sign of the result [-Wsign-conversion]
  447 |     return adaptive_counter_bits(value, backoff);
      |                                  ^~~~~
/usr/include/python3.12/internal/pycore_code.h:447:41: warning: conversion to ‘int’ from ‘unsigned int’ may change the sign of the result [-Wsign-conversion]
  447 |     return adaptive_counter_bits(value, backoff);

A tentative (untested) patch would be the following:

diff --git a/Include/internal/pycore_code.h b/Include/internal/pycore_code.h
index d1829eb324..a7b22968ba 100644
--- a/Include/internal/pycore_code.h
+++ b/Include/internal/pycore_code.h
@@ -427,9 +427,9 @@ write_location_entry_start(uint8_t *ptr, int code, int length)
 
 
 static inline uint16_t
-adaptive_counter_bits(int value, int backoff) {
-    return (value << ADAPTIVE_BACKOFF_BITS) |
-        (backoff & ((1<<ADAPTIVE_BACKOFF_BITS)-1));
+adaptive_counter_bits(unsigned int value, unsigned int backoff) {
+    return (uint16_t) ((value << ADAPTIVE_BACKOFF_BITS) |
+                       (backoff & ((1U<<ADAPTIVE_BACKOFF_BITS)-1U)));
 }
 
 static inline uint16_t
@@ -446,12 +446,12 @@ adaptive_counter_cooldown(void) {
 
 static inline uint16_t
 adaptive_counter_backoff(uint16_t counter) {
-    unsigned int backoff = counter & ((1<<ADAPTIVE_BACKOFF_BITS)-1);
+    unsigned int backoff = counter & ((1U<<ADAPTIVE_BACKOFF_BITS)-1U);
     backoff++;
     if (backoff > MAX_BACKOFF_VALUE) {
         backoff = MAX_BACKOFF_VALUE;
     }
-    unsigned int value = (1 << backoff) - 1;
+    unsigned int value = (1U << backoff) - 1U;
     return adaptive_counter_bits(value, backoff);
 }

If this approach of adding an explicit (uint16_t) cast and adding U suffices to integer literals where needed is OK with you, I can provide a full and tested patch or pull request.

Your environment

  • CPython versions tested on: Python 3.12.0b3
  • Operating system and architecture: Linux Fedora 38, GCC 13.1.1

Linked PRs

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    on Jul 8, 2023
  2. added 3 commits that reference this issue on Dec 2, 2023
  3. added a commit that references this issue on Dec 4, 2023
  4. added a commit that references this issue on Dec 4, 2023
  5. vstinner commented on Dec 4, 2023

    Member

    Thanks for the bug report, it should not be fixed. Until a Python version including the change is released, you should avoid the -Wconversion compiler flag (or find a way to ignore warnings from Python header files).

  6. added a commit that references this issue on Feb 11, 2024
  7. added a commit that references this issue on Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)topic-C-APItype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL