Skip to contentSkip to conversation

eal/windows: fix out-of-memory check

PATCH Superseded Dmitry Kozlyuk 3 participants 4 messages Feb 17, 2020 – Feb 21, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 eal/windows: fix out-of-memory check

Check vsnprintf() result to prevent calling malloc() with negative size. Check actual malloc() result and terminate asprintf() with documented error code to prevent the use of NULL pointer.

Fixes e8428a9d8 ("eal/windows: add some basic functions and macros")
---
 lib/librte_eal/windows/eal/include/rte_os.h | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
lib/librte_eal/windows/eal/include/rte_os.h +4 -2
index 9e762617b..69e8f4733 100644
@@ -66,10 +66,12 @@ asprintf(char **buffer, const char *format, ...)
 	va_start(arg, format);
 	size = vsnprintf(NULL, 0, format, arg) + 1;
 	va_end(arg);
+	if (size < 0)
+		return -1;
 
 	*buffer = malloc(size);
-	if (buffer == NULL)
-		printf("Cannot allocate memory");
+	if (*buffer == NULL)
+		return -1;
 
 	va_start(arg, format);
 	ret = vsnprintf(*buffer, size, format, arg);
Signature · 1 line
2.25.0
#1 lore raw

PATCH v2 1/1 eal/windows: fix out-of-memory check

Check vsnprintf() result to prevent calling malloc() with negative size. Check actual malloc() result and terminate asprintf() with documented error code to prevent the use of NULL pointer.

Fixes e8428a9d8 ("eal/windows: add some basic functions and macros")
---
 lib/librte_eal/windows/eal/include/rte_os.h | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

v2 Changes:
Check vsnprintf() value before appending place for '\0'.

lib/librte_eal/windows/eal/include/rte_os.h +6 -3
index 9e762617b..c76be1216 100644
@@ -64,12 +64,15 @@ asprintf(char **buffer, const char *format, ...)
 	va_list arg;
 
 	va_start(arg, format);
-	size = vsnprintf(NULL, 0, format, arg) + 1;
+	size = vsnprintf(NULL, 0, format, arg);
 	va_end(arg);
+	if (size < 0)
+		return -1;
+	size++;
 
 	*buffer = malloc(size);
-	if (buffer == NULL)
-		printf("Cannot allocate memory");
+	if (*buffer == NULL)
+		return -1;
 
 	va_start(arg, format);
 	ret = vsnprintf(*buffer, size, format, arg);
Signature · 1 line
2.25.0
Acked-by on v2 1/1
#2 lore raw
Quoted 6 lines · Dmitry Kozlyuk

Check vsnprintf() result to prevent calling malloc() with negative size. Check actual malloc() result and terminate asprintf() with documented error code to prevent the use of NULL pointer.

Fixes e8428a9d8 ("eal/windows: add some basic functions and macros")
Quoted 8 lines

---
  lib/librte_eal/windows/eal/include/rte_os.h | 9 ++++++---

  1 file changed, 6 insertions(+), 3 deletions(-)

v2 Changes:
     Check vsnprintf() value before appending place for '\0'.

#3 lore raw

18/02/2020 22:52, Ranjit Menon:

Quoted 12 lines · Dmitry Kozlyuk
Quoted 11 lines

Check vsnprintf() result to prevent calling malloc() with negative size. Check actual malloc() result and terminate asprintf() with documented error code to prevent the use of NULL pointer.

Fixes e8428a9d8 ("eal/windows: add some basic functions and macros")
---
  lib/librte_eal/windows/eal/include/rte_os.h | 9 ++++++---
  1 file changed, 6 insertions(+), 3 deletions(-)
Quoted 4 lines
Quoted 3 lines

v2 Changes:
Check vsnprintf() value before appending place for '\0'.

Applied, thanks.

Note: adding "in asprintf" to the title in order to give context.