Skip to content

Commit b3aff7d

Browse files
committed
Fix Index-out-of-bounds in IntegerMatcher::UpdateTablesForFeature
This fixes issue #2299, an issue which was already reported by static code analyzers and now by OSS-Fuzz, see details at https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=13597. The Tesseract code assigns an address which is out-of-bounds to a pointer variable, but increments that variable later. So this is a false positive. Change the code nevertheless to satisfy OSS-Fuzz. Signed-off-by: Stefan Weil <[email protected]>
1 parent 91d0a71 commit b3aff7d

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

src/classify/intmatcher.cpp

+9-11
Original file line numberDiff line numberDiff line change
@@ -767,8 +767,6 @@ int IntegerMatcher::UpdateTablesForFeature(
767767
uint8_t proto_byte;
768768
int32_t proto_word_offset;
769769
int32_t proto_offset;
770-
uint8_t config_byte;
771-
int32_t config_offset;
772770
PROTO_SET ProtoSet;
773771
uint32_t *ProtoPrunerPtr;
774772
INT_PROTO Proto;
@@ -777,7 +775,6 @@ int IntegerMatcher::UpdateTablesForFeature(
777775
uint32_t XFeatureAddress;
778776
uint32_t YFeatureAddress;
779777
uint32_t ThetaFeatureAddress;
780-
uint8_t* UINT8Pointer;
781778
int ProtoIndex;
782779
uint8_t Temp;
783780
int* IntPointer;
@@ -850,21 +847,22 @@ int IntegerMatcher::UpdateTablesForFeature(
850847

851848
ConfigWord &= *ConfigMask;
852849

853-
UINT8Pointer = tables->feature_evidence_ - 8;
854-
config_byte = 0;
850+
uint8_t feature_evidence_index = 0;
851+
uint8_t config_byte = 0;
855852
while (ConfigWord != 0 || config_byte != 0) {
856853
while (config_byte == 0) {
857854
config_byte = ConfigWord & 0xff;
858855
ConfigWord >>= 8;
859-
UINT8Pointer += 8;
856+
feature_evidence_index += 8;
860857
}
861-
config_offset = offset_table[config_byte];
858+
const uint8_t config_offset =
859+
offset_table[config_byte] + feature_evidence_index - 8;
862860
config_byte = next_table[config_byte];
863-
if (Evidence > UINT8Pointer[config_offset])
864-
UINT8Pointer[config_offset] = Evidence;
861+
if (Evidence > tables->feature_evidence_[config_offset])
862+
tables->feature_evidence_[config_offset] = Evidence;
865863
}
866864

867-
UINT8Pointer =
865+
uint8_t* UINT8Pointer =
868866
&(tables->proto_evidence_[ActualProtoNum + proto_offset][0]);
869867
for (ProtoIndex =
870868
ClassTemplate->ProtoLengths[ActualProtoNum + proto_offset];
@@ -888,7 +886,7 @@ int IntegerMatcher::UpdateTablesForFeature(
888886
}
889887

890888
IntPointer = tables->sum_feature_evidence_;
891-
UINT8Pointer = tables->feature_evidence_;
889+
uint8_t* UINT8Pointer = tables->feature_evidence_;
892890
int SumOverConfigs = 0;
893891
for (ConfigNum = ClassTemplate->NumConfigs; ConfigNum > 0; ConfigNum--) {
894892
int evidence = *UINT8Pointer++;

0 commit comments

Comments
 (0)