浏览代码

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 年之前
父节点
当前提交
c5965c8605
共有 1 个文件被更改,包括 18 次插入4 次删除
  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",