@@ -320,13 +320,11 @@ encode_varint_i32(uint8_t *buf, int32_t value)
320320 return encode_varint_u32 (buf , zigzag );
321321}
322322
323- /* Decode unsigned 64-bit varint. Updates offset only on success. Returns value .
323+ /* Decode unsigned 64-bit varint (LEB128) . Updates offset only on success.
324324 * On error (overflow or incomplete), offset is NOT updated, allowing callers
325- * to detect errors via (offset == prev_offset) check.
326- * On success, sets *error to 0 if error is non-NULL.
327- * On error, sets *error to 1 if error is non-NULL. */
325+ * to detect errors via (offset == prev_offset) check. Sets PyErr on error. */
328326static inline uint64_t
329- decode_varint_u64_ex (const uint8_t * data , size_t * offset , size_t max_size , int * error )
327+ decode_varint_u64 (const uint8_t * data , size_t * offset , size_t max_size )
330328{
331329 size_t pos = * offset ;
332330 uint64_t result = 0 ;
@@ -335,7 +333,6 @@ decode_varint_u64_ex(const uint8_t *data, size_t *offset, size_t max_size, int *
335333 /* Fast path for single-byte varints (0-127) - most common case */
336334 if (LIKELY (pos < max_size && (data [pos ] & 0x80 ) == 0 )) {
337335 * offset = pos + 1 ;
338- if (error ) * error = 0 ;
339336 return data [pos ];
340337 }
341338
@@ -344,87 +341,45 @@ decode_varint_u64_ex(const uint8_t *data, size_t *offset, size_t max_size, int *
344341 result |= (uint64_t )(byte & 0x7F ) << shift ;
345342 if ((byte & 0x80 ) == 0 ) {
346343 * offset = pos ;
347- if (error ) * error = 0 ;
348344 return result ;
349345 }
350346 shift += 7 ;
351347 if (UNLIKELY (shift >= 64 )) {
352- /* Overflow - do NOT update offset so caller can detect error */
353- if (error ) * error = 1 ;
348+ PyErr_SetString (PyExc_ValueError , "Invalid or incomplete varint in binary data" );
354349 return 0 ;
355350 }
356351 }
357352
358- /* Incomplete varint - do NOT update offset so caller can detect error */
359- if (error ) * error = 1 ;
353+ PyErr_SetString (PyExc_ValueError , "Invalid or incomplete varint in binary data" );
360354 return 0 ;
361355}
362356
363- /* Backward-compatible wrapper that sets PyErr on error.
364- * Callers should check PyErr_Occurred() after batch operations. */
365- static inline uint64_t
366- decode_varint_u64 (const uint8_t * data , size_t * offset , size_t max_size )
367- {
368- int error = 0 ;
369- uint64_t result = decode_varint_u64_ex (data , offset , max_size , & error );
370- if (UNLIKELY (error )) {
371- PyErr_SetString (PyExc_ValueError , "Invalid or incomplete varint in binary data" );
372- }
373- return result ;
374- }
375-
376- /* Decode unsigned 32-bit varint with explicit error handling.
377- * If value exceeds UINT32_MAX, treats as error: offset is NOT updated,
378- * *error is set to 1, allowing callers to detect via (offset == prev_offset). */
357+ /* Decode unsigned 32-bit varint. If value exceeds UINT32_MAX, treats as error. */
379358static inline uint32_t
380- decode_varint_u32_ex (const uint8_t * data , size_t * offset , size_t max_size , int * error )
359+ decode_varint_u32 (const uint8_t * data , size_t * offset , size_t max_size )
381360{
382361 size_t saved_offset = * offset ;
383- uint64_t value = decode_varint_u64_ex (data , offset , max_size , error );
384- if (error && * error ) {
385- /* decode_varint_u64_ex already handled the error, offset unchanged */
362+ uint64_t value = decode_varint_u64 (data , offset , max_size );
363+ if (PyErr_Occurred ()) {
386364 return 0 ;
387365 }
388366 if (UNLIKELY (value > UINT32_MAX )) {
389- /* Value overflow - restore offset so caller can detect error */
390367 * offset = saved_offset ;
391- if ( error ) * error = 1 ;
368+ PyErr_SetString ( PyExc_ValueError , "Invalid or incomplete varint in binary data" ) ;
392369 return 0 ;
393370 }
394371 return (uint32_t )value ;
395372}
396373
397- /* Backward-compatible wrapper that sets PyErr on error. */
398- static inline uint32_t
399- decode_varint_u32 (const uint8_t * data , size_t * offset , size_t max_size )
400- {
401- int error = 0 ;
402- uint32_t result = decode_varint_u32_ex (data , offset , max_size , & error );
403- if (UNLIKELY (error )) {
404- PyErr_SetString (PyExc_ValueError , "Invalid or incomplete varint in binary data" );
405- }
406- return result ;
407- }
408-
409- /* Decode signed 32-bit varint (zigzag) with explicit error handling. */
410- static inline int32_t
411- decode_varint_i32_ex (const uint8_t * data , size_t * offset , size_t max_size , int * error )
412- {
413- uint32_t zigzag = decode_varint_u32_ex (data , offset , max_size , error );
414- /* Zigzag decode */
415- return (int32_t )((zigzag >> 1 ) ^ - (int32_t )(zigzag & 1 ));
416- }
417-
418- /* Backward-compatible wrapper that sets PyErr on error. */
374+ /* Decode signed 32-bit varint (zigzag encoding). */
419375static inline int32_t
420376decode_varint_i32 (const uint8_t * data , size_t * offset , size_t max_size )
421377{
422- int error = 0 ;
423- int32_t result = decode_varint_i32_ex (data , offset , max_size , & error );
424- if (UNLIKELY (error )) {
425- PyErr_SetString (PyExc_ValueError , "Invalid or incomplete varint in binary data" );
378+ uint32_t zigzag = decode_varint_u32 (data , offset , max_size );
379+ if (PyErr_Occurred ()) {
380+ return 0 ;
426381 }
427- return result ;
382+ return ( int32_t )(( zigzag >> 1 ) ^ - ( int32_t )( zigzag & 1 )) ;
428383}
429384
430385/* ============================================================================
0 commit comments