Add check for integer overflow in odr_write YAZ-816
[yaz-moved-to-github.git] / src / odr_mem.c
index 71a10e1..9d502df 100644 (file)
@@ -10,6 +10,7 @@
 #include <config.h>
 #endif
 
 #include <config.h>
 #endif
 
+#include <limits.h>
 #include <stdlib.h>
 #include "odr-priv.h"
 #include <yaz/xmalloc.h>
 #include <stdlib.h>
 #include "odr-priv.h"
 #include <yaz/xmalloc.h>
@@ -96,6 +97,11 @@ int odr_grow_block(ODR b, int min_bytes)
 
 int odr_write(ODR o, const char *buf, int bytes)
 {
 
 int odr_write(ODR o, const char *buf, int bytes)
 {
+    if (bytes < 0 || o->op->pos > INT_MAX - bytes)
+    {
+        odr_seterror(o, OSPACE, 40);
+        return -1;
+    }
     if (o->op->pos + bytes >= o->op->size && odr_grow_block(o, bytes))
     {
         odr_seterror(o, OSPACE, 40);
     if (o->op->pos + bytes >= o->op->size && odr_grow_block(o, bytes))
     {
         odr_seterror(o, OSPACE, 40);