mirror of https://github.com/ipxe/ipxe.git
[settings] Allow numeric_setting_value() to handle long setting values
Allow numeric_setting_value() to handle e.g. the byte sequence 00:00:00:00:12:34:56:78 by returning -ERANGE only if the value actually overflows the return type. Signed-off-by: Michael Brown <mcb30@ipxe.org>pull/598/head
parent
d105627928
commit
55daa953fb
|
@ -862,18 +862,18 @@ static int numeric_setting_value ( int is_signed, const void *raw, size_t len,
|
||||||
const int8_t *signed_bytes = raw;
|
const int8_t *signed_bytes = raw;
|
||||||
int is_negative;
|
int is_negative;
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
|
uint8_t pad;
|
||||||
uint8_t byte;
|
uint8_t byte;
|
||||||
|
|
||||||
/* Range check */
|
|
||||||
if ( len > sizeof ( long ) )
|
|
||||||
return -ERANGE;
|
|
||||||
|
|
||||||
/* Convert to host-ordered longs */
|
/* Convert to host-ordered longs */
|
||||||
is_negative = ( len && ( signed_bytes[0] < 0 ) );
|
is_negative = ( len && ( signed_bytes[0] < 0 ) );
|
||||||
*value = ( ( is_signed && is_negative ) ? -1L : 0 );
|
*value = ( ( is_signed && is_negative ) ? -1L : 0 );
|
||||||
|
pad = *value;
|
||||||
for ( i = 0 ; i < len ; i++ ) {
|
for ( i = 0 ; i < len ; i++ ) {
|
||||||
byte = unsigned_bytes[i];
|
byte = unsigned_bytes[i];
|
||||||
*value = ( ( *value << 8 ) | byte );
|
*value = ( ( *value << 8 ) | byte );
|
||||||
|
if ( ( ( i + sizeof ( *value ) ) < len ) && ( byte != pad ) )
|
||||||
|
return -ERANGE;
|
||||||
}
|
}
|
||||||
|
|
||||||
return len;
|
return len;
|
||||||
|
|
|
@ -342,6 +342,12 @@ static void settings_test_exec ( void ) {
|
||||||
RAW ( 0x98, 0xab, 0x41, 0x81 ), 0x98ab4181 );
|
RAW ( 0x98, 0xab, 0x41, 0x81 ), 0x98ab4181 );
|
||||||
fetchn_ok ( &test_settings, &test_uint32_setting,
|
fetchn_ok ( &test_settings, &test_uint32_setting,
|
||||||
RAW ( 0xff, 0xff, 0xfe ), 0x00fffffe );
|
RAW ( 0xff, 0xff, 0xfe ), 0x00fffffe );
|
||||||
|
fetchn_ok ( &test_settings, &test_uint32_setting,
|
||||||
|
RAW ( 0, 0, 0, 0x12, 0x34, 0x56, 0x78 ), 0x12345678 );
|
||||||
|
fetchn_ok ( &test_settings, &test_int32_setting,
|
||||||
|
RAW ( 0, 0, 0, 0x12, 0x34, 0x56, 0x78 ), 0x12345678 );
|
||||||
|
fetchn_ok ( &test_settings, &test_int32_setting,
|
||||||
|
RAW ( 0xff, 0xff, 0x87, 0x65, 0x43, 0x21 ), -0x789abcdf );
|
||||||
|
|
||||||
/* "hex" setting type */
|
/* "hex" setting type */
|
||||||
storef_ok ( &test_settings, &test_hex_setting,
|
storef_ok ( &test_settings, &test_hex_setting,
|
||||||
|
|
Loading…
Reference in New Issue