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