Skip to content

Commit dd3f730

Browse files
jxy-smiloyip
authored andcommitted
Make schema dtor robust against exceptions
1 parent 781a4e6 commit dd3f730

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

include/rapidjson/schema.h

+12-4
Original file line numberDiff line numberDiff line change
@@ -380,13 +380,19 @@ struct SchemaValidationContext {
380380
if (hasher)
381381
factory.DestroryHasher(hasher);
382382
if (validators) {
383-
for (SizeType i = 0; i < validatorCount; i++)
384-
factory.DestroySchemaValidator(validators[i]);
383+
for (SizeType i = 0; i < validatorCount; i++) {
384+
if (validators[i]) {
385+
factory.DestroySchemaValidator(validators[i]);
386+
}
387+
}
385388
factory.FreeState(validators);
386389
}
387390
if (patternPropertiesValidators) {
388-
for (SizeType i = 0; i < patternPropertiesValidatorCount; i++)
389-
factory.DestroySchemaValidator(patternPropertiesValidators[i]);
391+
for (SizeType i = 0; i < patternPropertiesValidatorCount; i++) {
392+
if (patternPropertiesValidators[i]) {
393+
factory.DestroySchemaValidator(patternPropertiesValidators[i]);
394+
}
395+
}
390396
factory.FreeState(patternPropertiesValidators);
391397
}
392398
if (patternPropertiesSchemas)
@@ -1301,6 +1307,7 @@ class Schema {
13011307
if (validatorCount_) {
13021308
RAPIDJSON_ASSERT(context.validators == 0);
13031309
context.validators = static_cast<ISchemaValidator**>(context.factory.MallocState(sizeof(ISchemaValidator*) * validatorCount_));
1310+
std::memset(context.validators, 0, sizeof(ISchemaValidator*) * validatorCount_);
13041311
context.validatorCount = validatorCount_;
13051312

13061313
// Always return after first failure for these sub-validators
@@ -2544,6 +2551,7 @@ RAPIDJSON_MULTILINEMACRO_END
25442551
ISchemaValidator**& va = CurrentContext().patternPropertiesValidators;
25452552
SizeType& validatorCount = CurrentContext().patternPropertiesValidatorCount;
25462553
va = static_cast<ISchemaValidator**>(MallocState(sizeof(ISchemaValidator*) * count));
2554+
std::memset(va, 0, sizeof(ISchemaValidator*) * count);
25472555
for (SizeType i = 0; i < count; i++)
25482556
va[validatorCount++] = CreateSchemaValidator(*sa[i], true); // Inherit continueOnError
25492557
}

0 commit comments

Comments
 (0)