Prechádzať zdrojové kódy

libobs: Log errors for bmalloc(0)

malloc(0) is implementation defined and almost always a mistake. I
believe we should be more like malloc and treat bmalloc(0) as an error
instead of tying to handle it. For now we can log an error in case 3rd
party plugins are broken, in the future I would suggest this becomes a
crash-worthy error.
Richard Stanway 3 rokov pred
rodič
commit
c5965c8605
1 zmenil súbory, kde vykonal 18 pridanie a 4 odobranie
  1. 18 4
      libobs/util/bmem.c

+ 18 - 4
libobs/util/bmem.c

@@ -91,9 +91,16 @@ static long num_allocs = 0;
 
 void *bmalloc(size_t size)
 {
+	if (!size) {
+		blog(LOG_ERROR,
+		     "bmalloc: Allocating 0 bytes is broken behavior, please "
+		     "fix your code! This will crash in future versions of "
+		     "OBS.");
+		size = 1;
+	}
+
 	void *ptr = a_malloc(size);
-	if (!ptr && !size)
-		ptr = a_malloc(1);
+
 	if (!ptr) {
 		os_breakpoint();
 		bcrash("Out of memory while trying to allocate %lu bytes",
@@ -109,9 +116,16 @@ void *brealloc(void *ptr, size_t size)
 	if (!ptr)
 		os_atomic_inc_long(&num_allocs);
 
+	if (!size) {
+		blog(LOG_ERROR,
+		     "brealloc: Allocating 0 bytes is broken behavior, please "
+		     "fix your code! This will crash in future versions of "
+		     "OBS.");
+		size = 1;
+	}
+
 	ptr = a_realloc(ptr, size);
-	if (!ptr && !size)
-		ptr = a_realloc(ptr, 1);
+
 	if (!ptr) {
 		os_breakpoint();
 		bcrash("Out of memory while trying to allocate %lu bytes",