44#include <string.h>
55#include <math.h>
66
7+ static bool checked_size_to_u32 (size_t value , uint32_t * out ) {
8+ if (value > UINT32_MAX ) {
9+ return false;
10+ }
11+ * out = (uint32_t )value ;
12+ return true;
13+ }
14+
15+ static ironcfg_error_t bounds_error (size_t offset ) {
16+ ironcfg_error_t err = { IRONCFG_BOUNDS_VIOLATION , 0 };
17+ (void )checked_size_to_u32 (offset , & err .offset );
18+ return err ;
19+ }
20+
721/* CRC32 lookup table (IEEE 802.3) */
822static uint32_t crc32_table [256 ];
923static int crc32_table_initialized = 0 ;
@@ -15,7 +29,11 @@ static void crc32_init_table(void) {
1529 for (int i = 0 ; i < 256 ; i ++ ) {
1630 uint32_t crc = i ;
1731 for (int j = 0 ; j < 8 ; j ++ ) {
18- crc = (crc >> 1 ) ^ (0xEDB88320 & (- (crc & 1 )));
32+ if ((crc & 1U ) != 0U ) {
33+ crc = (crc >> 1 ) ^ 0xEDB88320U ;
34+ } else {
35+ crc >>= 1 ;
36+ }
1937 }
2038 crc32_table [i ] = crc ;
2139 }
@@ -88,13 +106,13 @@ void ironcfg_encode_varuint(uint8_t *buffer, size_t offset, uint64_t value, uint
88106 value >>= 7 ;
89107 }
90108 buffer [pos ++ ] = value & 0x7F ;
91- * out_size = pos - offset ;
109+ * out_size = ( uint32_t )( pos - offset ) ;
92110}
93111
94112/* Encode boolean value */
95113static ironcfg_error_t encode_bool (ironcfg_encode_ctx_t * ctx , const ironcfg_value_t * val ) {
96114 if (ctx -> offset + 1 > ctx -> buffer_size ) {
97- return ( ironcfg_error_t ){ IRONCFG_BOUNDS_VIOLATION , ctx -> offset } ;
115+ return bounds_error ( ctx -> offset ) ;
98116 }
99117 ctx -> buffer [ctx -> offset ++ ] = val -> data .bool_val .value ? 0x02 : 0x01 ;
100118 return (ironcfg_error_t ){IRONCFG_OK , 0 };
@@ -103,7 +121,7 @@ static ironcfg_error_t encode_bool(ironcfg_encode_ctx_t *ctx, const ironcfg_valu
103121/* Encode i64 value */
104122static ironcfg_error_t encode_i64 (ironcfg_encode_ctx_t * ctx , const ironcfg_value_t * val ) {
105123 if (ctx -> offset + 9 > ctx -> buffer_size ) {
106- return ( ironcfg_error_t ){ IRONCFG_BOUNDS_VIOLATION , ctx -> offset } ;
124+ return bounds_error ( ctx -> offset ) ;
107125 }
108126 ctx -> buffer [ctx -> offset ++ ] = 0x10 ;
109127 write_le64 (& ctx -> buffer [ctx -> offset ], (uint64_t )val -> data .i64_val .value );
@@ -114,7 +132,7 @@ static ironcfg_error_t encode_i64(ironcfg_encode_ctx_t *ctx, const ironcfg_value
114132/* Encode u64 value */
115133static ironcfg_error_t encode_u64 (ironcfg_encode_ctx_t * ctx , const ironcfg_value_t * val ) {
116134 if (ctx -> offset + 9 > ctx -> buffer_size ) {
117- return ( ironcfg_error_t ){ IRONCFG_BOUNDS_VIOLATION , ctx -> offset } ;
135+ return bounds_error ( ctx -> offset ) ;
118136 }
119137 ctx -> buffer [ctx -> offset ++ ] = 0x11 ;
120138 write_le64 (& ctx -> buffer [ctx -> offset ], val -> data .u64_val .value );
@@ -125,14 +143,14 @@ static ironcfg_error_t encode_u64(ironcfg_encode_ctx_t *ctx, const ironcfg_value
125143/* Encode f64 value */
126144static ironcfg_error_t encode_f64 (ironcfg_encode_ctx_t * ctx , const ironcfg_value_t * val ) {
127145 if (ctx -> offset + 9 > ctx -> buffer_size ) {
128- return ( ironcfg_error_t ){ IRONCFG_BOUNDS_VIOLATION , ctx -> offset } ;
146+ return bounds_error ( ctx -> offset ) ;
129147 }
130148
131149 double value = val -> data .f64_val .value ;
132150
133151 /* Check for NaN */
134152 if (ironcfg_is_nan (value )) {
135- return (ironcfg_error_t ){IRONCFG_INVALID_FLOAT , ctx -> offset };
153+ return (ironcfg_error_t ){IRONCFG_INVALID_FLOAT , bounds_error ( ctx -> offset ). offset };
136154 }
137155
138156 /* Normalize -0.0 to +0.0 */
@@ -153,7 +171,7 @@ static ironcfg_error_t encode_string(ironcfg_encode_ctx_t *ctx, const ironcfg_va
153171 const uint32_t len = val -> data .string_val .len ;
154172
155173 if (ctx -> offset + 1 + 5 + len > ctx -> buffer_size ) {
156- return ( ironcfg_error_t ){ IRONCFG_BOUNDS_VIOLATION , ctx -> offset } ;
174+ return bounds_error ( ctx -> offset ) ;
157175 }
158176
159177 ctx -> buffer [ctx -> offset ++ ] = 0x20 ;
@@ -171,7 +189,7 @@ static ironcfg_error_t encode_bytes(ironcfg_encode_ctx_t *ctx, const ironcfg_val
171189 const uint32_t len = val -> data .bytes_val .len ;
172190
173191 if (ctx -> offset + 1 + 5 + len > ctx -> buffer_size ) {
174- return ( ironcfg_error_t ){ IRONCFG_BOUNDS_VIOLATION , ctx -> offset } ;
192+ return bounds_error ( ctx -> offset ) ;
175193 }
176194
177195 ctx -> buffer [ctx -> offset ++ ] = 0x22 ;
@@ -189,7 +207,7 @@ static ironcfg_error_t encode_array(ironcfg_encode_ctx_t *ctx, const ironcfg_val
189207 const uint32_t count = val -> data .array_val .element_count ;
190208
191209 if (ctx -> offset + 1 + 5 > ctx -> buffer_size ) {
192- return ( ironcfg_error_t ){ IRONCFG_BOUNDS_VIOLATION , ctx -> offset } ;
210+ return bounds_error ( ctx -> offset ) ;
193211 }
194212
195213 ctx -> buffer [ctx -> offset ++ ] = 0x30 ;
@@ -210,7 +228,7 @@ static ironcfg_error_t encode_object(ironcfg_encode_ctx_t *ctx, const ironcfg_va
210228 const uint32_t field_count = val -> data .object_val .field_count ;
211229
212230 if (ctx -> offset + 1 + 5 > ctx -> buffer_size ) {
213- return ( ironcfg_error_t ){ IRONCFG_BOUNDS_VIOLATION , ctx -> offset } ;
231+ return bounds_error ( ctx -> offset ) ;
214232 }
215233
216234 ctx -> buffer [ctx -> offset ++ ] = 0x40 ;
@@ -223,7 +241,7 @@ static ironcfg_error_t encode_object(ironcfg_encode_ctx_t *ctx, const ironcfg_va
223241 uint32_t field_id = val -> data .object_val .schema -> fields [i ].field_id ;
224242
225243 if (ctx -> offset + 5 > ctx -> buffer_size ) {
226- return ( ironcfg_error_t ){ IRONCFG_BOUNDS_VIOLATION , ctx -> offset } ;
244+ return bounds_error ( ctx -> offset ) ;
227245 }
228246
229247 ironcfg_encode_varuint (ctx -> buffer , ctx -> offset , field_id , & varuint_size );
@@ -239,13 +257,13 @@ static ironcfg_error_t encode_object(ironcfg_encode_ctx_t *ctx, const ironcfg_va
239257/* Encode generic value (definition) */
240258static ironcfg_error_t encode_value (ironcfg_encode_ctx_t * ctx , const ironcfg_value_t * val ) {
241259 if (!val ) {
242- return (ironcfg_error_t ){IRONCFG_INVALID_SCHEMA , ctx -> offset };
260+ return (ironcfg_error_t ){IRONCFG_INVALID_SCHEMA , bounds_error ( ctx -> offset ). offset };
243261 }
244262
245263 switch (val -> type ) {
246264 case IRONCFG_VAL_NULL :
247265 if (ctx -> offset >= ctx -> buffer_size ) {
248- return ( ironcfg_error_t ){ IRONCFG_BOUNDS_VIOLATION , ctx -> offset } ;
266+ return bounds_error ( ctx -> offset ) ;
249267 }
250268 ctx -> buffer [ctx -> offset ++ ] = 0x00 ;
251269 return (ironcfg_error_t ){IRONCFG_OK , 0 };
@@ -275,7 +293,7 @@ static ironcfg_error_t encode_value(ironcfg_encode_ctx_t *ctx, const ironcfg_val
275293 return encode_object (ctx , val );
276294
277295 default :
278- return (ironcfg_error_t ){IRONCFG_INVALID_SCHEMA , ctx -> offset };
296+ return (ironcfg_error_t ){IRONCFG_INVALID_SCHEMA , bounds_error ( ctx -> offset ). offset };
279297 }
280298}
281299
@@ -284,7 +302,7 @@ static ironcfg_error_t encode_schema(ironcfg_encode_ctx_t *ctx, ironcfg_schema_t
284302 uint32_t varuint_size ;
285303
286304 if (ctx -> offset + 5 > ctx -> buffer_size ) {
287- return ( ironcfg_error_t ){ IRONCFG_BOUNDS_VIOLATION , ctx -> offset } ;
305+ return bounds_error ( ctx -> offset ) ;
288306 }
289307
290308 /* Write field count */
@@ -296,34 +314,34 @@ static ironcfg_error_t encode_schema(ironcfg_encode_ctx_t *ctx, ironcfg_schema_t
296314
297315 /* Write fieldId */
298316 if (ctx -> offset + 5 > ctx -> buffer_size ) {
299- return ( ironcfg_error_t ){ IRONCFG_BOUNDS_VIOLATION , ctx -> offset } ;
317+ return bounds_error ( ctx -> offset ) ;
300318 }
301319 ironcfg_encode_varuint (ctx -> buffer , ctx -> offset , field -> field_id , & varuint_size );
302320 ctx -> offset += varuint_size ;
303321
304322 /* Write fieldNameLen */
305323 if (ctx -> offset + 5 > ctx -> buffer_size ) {
306- return ( ironcfg_error_t ){ IRONCFG_BOUNDS_VIOLATION , ctx -> offset } ;
324+ return bounds_error ( ctx -> offset ) ;
307325 }
308326 ironcfg_encode_varuint (ctx -> buffer , ctx -> offset , field -> field_name_len , & varuint_size );
309327 ctx -> offset += varuint_size ;
310328
311329 /* Write fieldName */
312330 if (ctx -> offset + field -> field_name_len > ctx -> buffer_size ) {
313- return ( ironcfg_error_t ){ IRONCFG_BOUNDS_VIOLATION , ctx -> offset } ;
331+ return bounds_error ( ctx -> offset ) ;
314332 }
315333 memcpy (& ctx -> buffer [ctx -> offset ], field -> field_name , field -> field_name_len );
316334 ctx -> offset += field -> field_name_len ;
317335
318336 /* Write fieldType */
319337 if (ctx -> offset + 1 > ctx -> buffer_size ) {
320- return ( ironcfg_error_t ){ IRONCFG_BOUNDS_VIOLATION , ctx -> offset } ;
338+ return bounds_error ( ctx -> offset ) ;
321339 }
322340 ctx -> buffer [ctx -> offset ++ ] = field -> field_type ;
323341
324342 /* Write isRequired */
325343 if (ctx -> offset + 1 > ctx -> buffer_size ) {
326- return ( ironcfg_error_t ){ IRONCFG_BOUNDS_VIOLATION , ctx -> offset } ;
344+ return bounds_error ( ctx -> offset ) ;
327345 }
328346 ctx -> buffer [ctx -> offset ++ ] = field -> is_required ;
329347 }
@@ -353,16 +371,28 @@ ironcfg_error_t ironcfg_encode(
353371 }
354372
355373 /* Encode schema */
356- uint32_t schema_offset = ctx .offset ;
374+ uint32_t schema_offset ;
375+ if (!checked_size_to_u32 (ctx .offset , & schema_offset )) {
376+ return (ironcfg_error_t ){IRONCFG_BOUNDS_VIOLATION , 0 };
377+ }
357378 ironcfg_error_t err = encode_schema (& ctx , schema );
358379 if (err .code != IRONCFG_OK ) return err ;
359- uint32_t schema_size = ctx .offset - schema_offset ;
380+ uint32_t schema_size ;
381+ if (!checked_size_to_u32 (ctx .offset - (size_t )schema_offset , & schema_size )) {
382+ return (ironcfg_error_t ){IRONCFG_BOUNDS_VIOLATION , 0 };
383+ }
360384
361385 /* Encode data */
362- uint32_t data_offset = ctx .offset ;
386+ uint32_t data_offset ;
387+ if (!checked_size_to_u32 (ctx .offset , & data_offset )) {
388+ return (ironcfg_error_t ){IRONCFG_BOUNDS_VIOLATION , 0 };
389+ }
363390 err = encode_value (& ctx , root );
364391 if (err .code != IRONCFG_OK ) return err ;
365- uint32_t data_size = ctx .offset - data_offset ;
392+ uint32_t data_size ;
393+ if (!checked_size_to_u32 (ctx .offset - (size_t )data_offset , & data_size )) {
394+ return (ironcfg_error_t ){IRONCFG_BOUNDS_VIOLATION , 0 };
395+ }
366396
367397 /* Calculate CRC and BLAKE3 */
368398 uint32_t crc_offset = 0 ;
@@ -371,17 +401,24 @@ ironcfg_error_t ironcfg_encode(
371401
372402 if (compute_crc32 ) {
373403 flags |= 0x01 ;
374- crc_offset = ctx .offset ;
404+ if (!checked_size_to_u32 (ctx .offset , & crc_offset )) {
405+ return bounds_error (ctx .offset );
406+ }
375407 ctx .offset += 4 ;
376408 }
377409
378410 if (compute_blake3 ) {
379411 flags |= 0x02 ;
380- blake3_offset = ctx .offset ;
412+ if (!checked_size_to_u32 (ctx .offset , & blake3_offset )) {
413+ return bounds_error (ctx .offset );
414+ }
381415 ctx .offset += 32 ;
382416 }
383417
384- uint32_t file_size = ctx .offset ;
418+ uint32_t file_size ;
419+ if (!checked_size_to_u32 (ctx .offset , & file_size )) {
420+ return (ironcfg_error_t ){IRONCFG_BOUNDS_VIOLATION , 0 };
421+ }
385422
386423 if (file_size > buffer_size ) {
387424 return (ironcfg_error_t ){IRONCFG_BOUNDS_VIOLATION , 0 };
0 commit comments