Преглед изворни кода

Initialize returned pointer to always trigger NULL checks in callers

Also avoid allocation roundtrip if the file is empty after the BOM
Palana пре 11 година
родитељ
комит
4bb4b859bf
1 измењених фајлова са 5 додато и 2 уклоњено
  1. 5 2
      libobs/util/platform.c

+ 5 - 2
libobs/util/platform.c

@@ -108,6 +108,8 @@ size_t os_fread_utf8(FILE *file, char **pstr)
 	size_t size_read;
 	size_t size_read;
 	size_t len = 0;
 	size_t len = 0;
 
 
+	*pstr = NULL;
+
 	fseeko(file, 0, SEEK_END);
 	fseeko(file, 0, SEEK_END);
 	size = (size_t)ftello(file);
 	size = (size_t)ftello(file);
 
 
@@ -125,6 +127,9 @@ size_t os_fread_utf8(FILE *file, char **pstr)
 		offset = (astrcmp_n(bom, "\xEF\xBB\xBF", 3) == 0) ? 3 : 0;
 		offset = (astrcmp_n(bom, "\xEF\xBB\xBF", 3) == 0) ? 3 : 0;
 
 
 		size -= offset;
 		size -= offset;
+		if (size == 0)
+			return 0;
+
 		utf8str = bmalloc(size+1);
 		utf8str = bmalloc(size+1);
 		fseeko(file, offset, SEEK_SET);
 		fseeko(file, offset, SEEK_SET);
 
 
@@ -137,8 +142,6 @@ size_t os_fread_utf8(FILE *file, char **pstr)
 		utf8str[size] = 0;
 		utf8str[size] = 0;
 
 
 		*pstr = utf8str;
 		*pstr = utf8str;
-	} else {
-		*pstr = NULL;
 	}
 	}
 
 
 	return len;
 	return len;