Blame SOURCES/49923ccb2143e36850bcdeb781e2bcdf5ce22f15.patch

4452fd
From 49923ccb2143e36850bcdeb781e2bcdf5ce22f15 Mon Sep 17 00:00:00 2001
4452fd
From: John Hawthorn <john@hawthorn.email>
4452fd
Date: Wed, 2 Mar 2022 14:17:59 -0800
4452fd
Subject: [PATCH] Check need < buf->used
4452fd
4452fd
We're guaranteed a power of 2 so that this becomes 0, but we might as
4452fd
well use a check for overflow that works in more cases.
4452fd
4452fd
Unsigned integer overflow is defined behaviour, so this should be safe.
4452fd
4452fd
(cherry picked from commit 36410d536b676e836637bb20574a56ebc920eb83)
4452fd
---
4452fd
 src/yajl_buf.c | 9 +++++++--
4452fd
 1 file changed, 7 insertions(+), 2 deletions(-)
4452fd
4452fd
diff --git a/src/yajl_buf.c b/src/yajl_buf.c
4452fd
index 1aeafde0..8bd1bea7 100644
4452fd
--- a/src/yajl_buf.c
4452fd
+++ b/src/yajl_buf.c
4452fd
@@ -30,7 +30,7 @@ struct yajl_buf_t {
4452fd
 };
4452fd
 
4452fd
 static
4452fd
-void yajl_buf_ensure_available(yajl_buf buf, size_t want)
4452fd
+int yajl_buf_ensure_available(yajl_buf buf, size_t want)
4452fd
 {
4452fd
     size_t need;
4452fd
     
4452fd
@@ -46,11 +46,15 @@ void yajl_buf_ensure_available(yajl_buf buf, size_t want)
4452fd
     need = buf->len;
4452fd
 
4452fd
     while (want >= (need - buf->used)) need <<= 1;
4452fd
+    if (need < buf->used) {
4452fd
+         return -1;
4452fd
+    }
4452fd
 
4452fd
     if (need != buf->len) {
4452fd
         buf->data = (unsigned char *) YA_REALLOC(buf->alloc, buf->data, need);
4452fd
         buf->len = need;
4452fd
     }
4452fd
+    return 0;
4452fd
 }
4452fd
 
4452fd
 yajl_buf yajl_buf_alloc(yajl_alloc_funcs * alloc)
4452fd
@@ -70,7 +74,8 @@ void yajl_buf_free(yajl_buf buf)
4452fd
 
4452fd
 void yajl_buf_append(yajl_buf buf, const void * data, size_t len)
4452fd
 {
4452fd
-    yajl_buf_ensure_available(buf, len);
4452fd
+    if (yajl_buf_ensure_available(buf, len))
4452fd
+        return;
4452fd
     if (len > 0) {
4452fd
         assert(data != NULL);
4452fd
         memcpy(buf->data + buf->used, data, len);