Skip to content

Commit c1da5fb

Browse files
committed
Fix CID 1164704 (Untrusted value as argument)
Limit the matrix to UINT16_MAX x UINT16_MAX. Larger dimensions could also result in an arithmetic overflow when multiplying the two dimensions. Signed-off-by: Stefan Weil <[email protected]>
1 parent 8871f4d commit c1da5fb

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/ccstruct/matrix.h

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* -*-C-*-
22
******************************************************************************
3-
* File: matrix.h (Formerly matrix.h)
3+
* File: matrix.h
44
* Description: Generic 2-d array/matrix and banded triangular matrix class.
55
* Author: Ray Smith
66
* TODO(rays) Separate from ratings matrix, which it also contains:
@@ -10,9 +10,6 @@
1010
* Author: Mark Seaman, OCR Technology
1111
* Created: Wed May 16 13:22:06 1990
1212
* Modified: Tue Mar 19 16:00:20 1991 (Mark Seaman) marks@hpgrlt
13-
* Language: C
14-
* Package: N/A
15-
* Status: Experimental (Do Not Distribute)
1613
*
1714
* (c) Copyright 1990, Hewlett-Packard Company.
1815
** Licensed under the Apache License, Version 2.0 (the "License");
@@ -492,13 +489,19 @@ class GENERIC_2D_ARRAY {
492489
ReverseN(&size1, sizeof(size1));
493490
ReverseN(&size2, sizeof(size2));
494491
}
492+
// Arbitrarily limit the number of elements to protect against bad data.
493+
if (size1 > UINT16_MAX) return false;
494+
if (size2 > UINT16_MAX) return false;
495495
Resize(size1, size2, empty_);
496496
return true;
497497
}
498498
bool DeSerializeSize(tesseract::TFile* fp) {
499499
int32_t size1, size2;
500500
if (fp->FReadEndian(&size1, sizeof(size1), 1) != 1) return false;
501501
if (fp->FReadEndian(&size2, sizeof(size2), 1) != 1) return false;
502+
// Arbitrarily limit the number of elements to protect against bad data.
503+
if (size1 > UINT16_MAX) return false;
504+
if (size2 > UINT16_MAX) return false;
502505
Resize(size1, size2, empty_);
503506
return true;
504507
}

0 commit comments

Comments
 (0)