Skip to content

Commit 8f16de1

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 bb1bae9 commit 8f16de1

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

scripts/minisat-2.2.1-patch

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,33 @@ 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+
@@ -32,8 +32,9 @@
250+
class OutOfMemoryException{};
251+
static inline void* xrealloc(void *ptr, size_t size)
252+
{
253+
+ errno = 0;
254+
void* mem = realloc(ptr, size);
255+
- if (mem == NULL && errno == ENOMEM){
256+
+ if (mem == NULL || errno == ENOMEM){
257+
throw OutOfMemoryException();
258+
}else
259+
return mem;

0 commit comments

Comments
 (0)