Skip to content

Commit 4f3c102

Browse files
committed
Silence Minisat's use of realloc on non-POD and fix and use its xrealloc
This was a new GCC 8 warning; testing on errno == ENOMEM with && opened the door for non-compliant implementations to fail to allocate without capacity() failing.
1 parent 3082d9d commit 4f3c102

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

scripts/minisat-2.2.1-patch

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,39 @@ index 9cbbc51..27b9700 100644
227227
#include <fpu_control.h>
228228
#endif
229229

230+
diff --git a/minisat/mtl/Vec.h b/minisat/mtl/Vec.h
231+
--- a/minisat/mtl/Vec.h
232+
+++ b/minisat/mtl/Vec.h
233+
@@ -96,8 +96,10 @@
234+
void vec<T>::capacity(int min_cap) {
235+
if (cap >= min_cap) return;
236+
int add = imax((min_cap - cap + 1) & ~1, ((cap >> 1) + 2) & ~1); // NOTE: grow by approximately 3/2
237+
- if (add > INT_MAX - cap || (((data = (T*)::realloc(data, (cap += add) * sizeof(T))) == NULL) && errno == ENOMEM))
238+
- throw OutOfMemoryException();
239+
+ if (add > INT_MAX - cap)
240+
+ throw OutOfMemoryException();
241+
+
242+
+ data = (T*)xrealloc(data, (cap += add) * sizeof(T));
243+
}
244+
245+
246+
diff --git a/minisat/mtl/XAlloc.h b/minisat/mtl/XAlloc.h
247+
--- a/minisat/mtl/XAlloc.h
248+
+++ b/minisat/mtl/XAlloc.h
249+
@@ -21,7 +21,6 @@
250+
#ifndef Minisat_XAlloc_h
251+
#define Minisat_XAlloc_h
252+
253+
-#include <errno.h>
254+
#include <stdlib.h>
255+
256+
namespace Minisat {
257+
@@ -33,7 +32,7 @@
258+
static inline void* xrealloc(void *ptr, size_t size)
259+
{
260+
void* mem = realloc(ptr, size);
261+
- if (mem == NULL && errno == ENOMEM){
262+
+ if (mem == NULL){
263+
throw OutOfMemoryException();
264+
}else
265+
return mem;

0 commit comments

Comments
 (0)