[v2] eal/windows: fix out-of-memory check

Message ID 20200217235616.85746-1-dmitry.kozliuk@gmail.com (mailing list archive)
State Accepted, archived
Delegated to: Thomas Monjalon
Headers
Series [v2] eal/windows: fix out-of-memory check |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/iol-testing success Testing PASS
ci/iol-mellanox-Performance success Performance Testing PASS
ci/travis-robot warning Travis build: failed
ci/Intel-compilation fail apply issues

Commit Message

Dmitry Kozlyuk Feb. 17, 2020, 11:56 p.m. UTC
  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")

Signed-off-by: Dmitry Kozlyuk <dmitry.kozliuk@gmail.com>
---
 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'.
  

Comments

Menon, Ranjit Feb. 18, 2020, 9:52 p.m. UTC | #1
On 2/17/2020 3:56 PM, Dmitry Kozlyuk wrote:
> 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")
>
> Signed-off-by: Dmitry Kozlyuk <dmitry.kozliuk@gmail.com>
> ---
>   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'.
>
Acked-by: Ranjit Menon <ranjit.menon@intel.com>
  
Thomas Monjalon Feb. 21, 2020, 4:37 p.m. UTC | #2
18/02/2020 22:52, Ranjit Menon:
> On 2/17/2020 3:56 PM, Dmitry Kozlyuk wrote:
> > 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")
> >
> > Signed-off-by: Dmitry Kozlyuk <dmitry.kozliuk@gmail.com>
> > ---
> >   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'.
> >
> Acked-by: Ranjit Menon <ranjit.menon@intel.com>

Applied, thanks.

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

Patch

diff --git a/lib/librte_eal/windows/eal/include/rte_os.h b/lib/librte_eal/windows/eal/include/rte_os.h
index 9e762617b..c76be1216 100644
--- a/lib/librte_eal/windows/eal/include/rte_os.h
+++ b/lib/librte_eal/windows/eal/include/rte_os.h
@@ -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);