Fix potential integer overflows identified by Coverity

Convert ODK_LAST_STRESSABLE_TYPE to an aliased enum value so that
ODK_FieldLength always returns a valid value instead of SIZE_MAX.

PiperOrigin-RevId: 602823670
Change-Id: I7a843cacca8201677c0f31249112c04f6c3e04cb
This commit is contained in:
Googler
2024-01-30 13:26:44 -08:00
committed by Robert Shih
parent b239b11b60
commit e53e8ced89
4 changed files with 7 additions and 9 deletions

View File

@@ -19,7 +19,7 @@ extern "C" {
#define ODK_MINOR_VERSION 0
/* ODK Version string. Date changed automatically on each release. */
#define ODK_RELEASE_DATE "ODK v19.0 2024-01-25"
#define ODK_RELEASE_DATE "ODK v19.0 2024-01-30"
/* The lowest version number for an ODK message. */
#define ODK_FIRST_VERSION 16

View File

@@ -258,7 +258,7 @@ TEST(OdkTest, SerializeFieldsStress) {
size_t total_size = 0;
for (int i = 0; i < n; i++) {
fields[i].type = static_cast<ODK_FieldType>(
std::rand() % static_cast<int>(ODK_LAST_STRESSABLE_TYPE));
std::rand() % (static_cast<int>(ODK_LAST_STRESSABLE_TYPE) + 1));
fields[i].value = malloc(ODK_AllocSize(fields[i].type));
fields[i].name = "stress";
total_size += ODK_FieldLength(fields[i].type);

View File

@@ -360,22 +360,20 @@ size_t ODK_FieldLength(ODK_FieldType type) {
return sizeof(uint64_t);
case ODK_INT64:
return sizeof(uint64_t);
case ODK_BOOL: // Booleans are stored in the message as 32 bit ints.
return sizeof(uint32_t);
case ODK_SUBSTRING:
return sizeof(uint32_t) + sizeof(uint32_t);
case ODK_DEVICEID:
return ODK_DEVICE_ID_LEN_MAX;
case ODK_MESSAGECOUNTER:
return ODK_MESSAGECOUNTERINFO_SIZE;
case ODK_DEVICEINFO:
return ODK_DEVICE_INFO_LEN_MAX;
case ODK_MESSAGECOUNTER:
return ODK_MESSAGECOUNTERINFO_SIZE;
case ODK_RENEWALDATA:
return ODK_KEYBOX_RENEWAL_DATA_SIZE;
case ODK_HASH:
return ODK_SHA256_HASH_SIZE;
default:
return SIZE_MAX;
case ODK_BOOL: // Booleans are stored in the message as 32 bit ints.
return sizeof(uint32_t);
}
}

View File

@@ -28,7 +28,7 @@ enum ODK_FieldType {
ODK_HASH,
// The "stressable" types are the ones we can put in a stress test that packs
// and unpacks random data and can expect to get back the same thing.
ODK_LAST_STRESSABLE_TYPE,
ODK_LAST_STRESSABLE_TYPE = ODK_HASH,
// Put boolean after ODK_LAST_STRESSABLE_TYPE, so that we skip boolean type in
// SerializeFieldsStress because we unpack any nonzero to 'true'.
ODK_BOOL,