Skip to content

Commit

Permalink
Added safety checks when malloc returns nil in GPBDescriptor
Browse files Browse the repository at this point in the history
  • Loading branch information
parveen-bhatia authored and thomasvl committed Nov 4, 2018
1 parent eecccdc commit 29f27bf
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions objectivec/GPBDescriptor.m
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,7 @@ - (void)calcValueNameOffsets {
return;
}
uint32_t *offsets = malloc(valueCount_ * sizeof(uint32_t));
if (!offsets) return;
const char *scan = valueNames_;
for (uint32_t i = 0; i < valueCount_; ++i) {
offsets[i] = (uint32_t)(scan - valueNames_);
Expand Down Expand Up @@ -851,6 +852,7 @@ - (BOOL)getValue:(int32_t *)outValue forEnumName:(NSString *)name {
nameAsCStr += prefixLen;

if (nameOffsets_ == NULL) [self calcValueNameOffsets];
if (nameOffsets_ == NULL) return NO;

// Find it.
for (uint32_t i = 0; i < valueCount_; ++i) {
Expand All @@ -867,6 +869,7 @@ - (BOOL)getValue:(int32_t *)outValue forEnumName:(NSString *)name {

- (BOOL)getValue:(int32_t *)outValue forEnumTextFormatName:(NSString *)textFormatName {
if (nameOffsets_ == NULL) [self calcValueNameOffsets];
if (nameOffsets_ == NULL) return NO;

for (uint32_t i = 0; i < valueCount_; ++i) {
int32_t value = values_[i];
Expand Down Expand Up @@ -905,6 +908,7 @@ - (uint32_t)enumNameCount {

- (NSString *)getEnumNameForIndex:(uint32_t)index {
if (nameOffsets_ == NULL) [self calcValueNameOffsets];
if (nameOffsets_ == NULL) return nil;

if (index >= valueCount_) {
return nil;
Expand All @@ -916,6 +920,7 @@ - (NSString *)getEnumNameForIndex:(uint32_t)index {

- (NSString *)getEnumTextFormatNameForIndex:(uint32_t)index {
if (nameOffsets_ == NULL) [self calcValueNameOffsets];
if (nameOffsets_ == NULL) return nil;

if (index >= valueCount_) {
return nil;
Expand Down

0 comments on commit 29f27bf

Please sign in to comment.