CVE-2020-19490: Integer Overflow or Wraparound (CWE-190)
value: tinyexr 0.9.5 has a integer overflow over-write in tinyexr::DecodePixelData in tinyexr.h, related to OpenEXR code.
CWE description: The product performs a calculation that can
         produce an integer overflow or wraparound when the logic
         assumes that the resulting value will always be larger than
         the original value. This occurs when an integer value is
         incremented to a value that is too large to store in the
         associated representation. When this occurs, the value may
         become a very small or negative number.
CVSS v3 base score: 5.5
Published: 2021-07-21T18:15Z
Repository: https://github.com/syoyo/tinyexr
Language: C
Files changed: tinyexr.h
Commit date: 2019-07-07 16:58:18 +0900
Commit message: Make line_no with too large value(2**20) invalid. Fixes #124

Diff: diff --git a/tinyexr.h b/tinyexr.h
index 564a0c0..f221637 100644
--- a/tinyexr.h
+++ b/tinyexr.h
@@ -470,11 +470,11 @@ extern int LoadEXRFromMemory(float **out_rgba, int *width, int *height,
 #include <cstdio>
 #include <cstdlib>
 #include <cstring>
 #include <sstream>
 
-// #include <iostream> // debug
+//#include <iostream> // debug
 
 #include <limits>
 #include <string>
 #include <vector>
 
@@ -7011,10 +7011,15 @@ static void swap2(unsigned short *val) {
 
 #ifdef __clang__
 #pragma clang diagnostic push
 #pragma clang diagnostic ignored "-Wunused-function"
 #endif
+
+#ifdef __GNUC__
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wunused-function"
+#endif
 static void cpy4(int *dst_val, const int *src_val) {
   unsigned char *dst = reinterpret_cast<unsigned char *>(dst_val);
   const unsigned char *src = reinterpret_cast<const unsigned char *>(src_val);
 
   dst[0] = src[0];
@@ -7044,10 +7049,14 @@ static void cpy4(float *dst_val, const float *src_val) {
 }
 #ifdef __clang__
 #pragma clang diagnostic pop
 #endif
 
+#ifdef __GNUC__
+#pragma GCC diagnostic pop
+#endif
+
 static void swap4(unsigned int *val) {
 #ifdef MINIZ_LITTLE_ENDIAN
   (void)val;
 #else
   unsigned int tmp = *val;
@@ -10947,10 +10956,15 @@ static int DecodeChunk(EXRImage *exr_image, const EXRHeader *exr_header,
         tinyexr::swap4(reinterpret_cast<unsigned int *>(&line_no));
         tinyexr::swap4(reinterpret_cast<unsigned int *>(&data_len));
 
         if (size_t(data_len) > data_size) {
           invalid_data = true;
+
+        } else if ((line_no > (2 << 20)) || (line_no < -(2 << 20))) {
+          // Too large value. Assume this is invalid
+          // 2**20 = 1048576 = heuristic value.
+          invalid_data = true;
         } else if (data_len == 0) {
           // TODO(syoyo): May be ok to raise the threshold for example `data_len
           // < 4`
           invalid_data = true;
         } else {

Vulnerable code: // #include <iostream> // debug
Fixed code: //#include <iostream> // debug

#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
#endif
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif


        } else if ((line_no > (2 << 20)) || (line_no < -(2 << 20))) {
          // Too large value. Assume this is invalid
          // 2**20 = 1048576 = heuristic value.
          invalid_data = true;