Repository navigation
_remote_debugging: magic and duplicate consts in binary format helpers #149300
Copy link
Copy link
Closed
Labels
extension-modulesC modules in the Modules dirC modules in the Modules dirtype-featureA feature request or enhancementA feature request or enhancement
Description
Feature or enhancement
Proposal:
This is a follow up to #148972 (comment)
In short, binary reader and writer could use some small clean up of consts.
There are some duplicates:
| #define MAX_DECOMPRESS_SIZE (1ULL << 30) |
| #define MAX_DECOMPRESS_SIZE (1ULL << 30) |
There are some magic numbers:
cpython/Modules/_remote_debugging/binary_io_writer.c
Lines 1146 to 1154 in b2f126c
| uint64_t file_size = (uint64_t)footer_offset + 32; | |
| uint8_t footer[32] = {0}; | |
| /* Cast size_t to uint32_t before memcpy to ensure correct bytes are copied | |
| * on both little-endian and big-endian systems (size_t is 8 bytes on 64-bit) */ | |
| uint32_t string_count_u32 = (uint32_t)writer->string_count; | |
| uint32_t frame_count_u32 = (uint32_t)writer->frame_count; | |
| memcpy(footer + 0, &string_count_u32, 4); | |
| memcpy(footer + 4, &frame_count_u32, 4); | |
| memcpy(footer + 8, &file_size, 8); |
There are definitely places in the module that handle it better, eg:
cpython/Modules/_remote_debugging/binary_io_writer.c
Lines 1174 to 1189 in 6b632ce
| uint8_t header[FILE_HEADER_SIZE] = {0}; | |
| uint32_t magic = BINARY_FORMAT_MAGIC; | |
| uint32_t version = BINARY_FORMAT_VERSION; | |
| memcpy(header + HDR_OFF_MAGIC, &magic, HDR_SIZE_MAGIC); | |
| memcpy(header + HDR_OFF_VERSION, &version, HDR_SIZE_VERSION); | |
| header[HDR_OFF_PY_MAJOR] = PY_MAJOR_VERSION; | |
| header[HDR_OFF_PY_MINOR] = PY_MINOR_VERSION; | |
| header[HDR_OFF_PY_MICRO] = PY_MICRO_VERSION; | |
| memcpy(header + HDR_OFF_START_TIME, &writer->start_time_us, HDR_SIZE_START_TIME); | |
| memcpy(header + HDR_OFF_INTERVAL, &writer->sample_interval_us, HDR_SIZE_INTERVAL); | |
| memcpy(header + HDR_OFF_SAMPLES, &writer->total_samples, HDR_SIZE_SAMPLES); | |
| memcpy(header + HDR_OFF_THREADS, &thread_count_u32, HDR_SIZE_THREADS); | |
| memcpy(header + HDR_OFF_STR_TABLE, &string_table_offset_u64, HDR_SIZE_STR_TABLE); | |
| memcpy(header + HDR_OFF_FRAME_TABLE, &frame_table_offset_u64, HDR_SIZE_FRAME_TABLE); | |
| memcpy(header + HDR_OFF_COMPRESSION, &compression_type_u32, HDR_SIZE_COMPRESSION); | |
| if (fwrite_checked_allow_threads(header, FILE_HEADER_SIZE, writer->fp) < 0) { |
Has this already been discussed elsewhere?
No response given
Links to previous discussion of this feature:
No response
Linked PRs
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
extension-modulesC modules in the Modules dirC modules in the Modules dirtype-featureA feature request or enhancementA feature request or enhancement
Activity
maurycy commentedon May 2, 2026
cc @pablogsal
gh-149300: `_remote_debugging`: clean up magic and duplicate consts i…