DPDK patches and discussions
 help / color / mirror / Atom feed
From: Dmitry Kozlyuk <dmitry.kozliuk@gmail.com>
To: dev@dpdk.org
Cc: Dmitry Kozlyuk <dmitry.kozliuk@gmail.com>,
	Harini Ramakrishnan <harini.ramakrishnan@microsoft.com>,
	Omar Cardona <ocardona@microsoft.com>,
	Pallavi Kadam <pallavi.kadam@intel.com>,
	Ranjit Menon <ranjit.menon@intel.com>
Subject: [dpdk-dev] [PATCH v2] eal/windows: fix out-of-memory check
Date: Tue, 18 Feb 2020 02:56:16 +0300	[thread overview]
Message-ID: <20200217235616.85746-1-dmitry.kozliuk@gmail.com> (raw)
In-Reply-To: <20200217022351.59429-1-dmitry.kozliuk@gmail.com>

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'.

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);
-- 
2.25.0


  reply	other threads:[~2020-02-17 23:56 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-17  2:23 [dpdk-dev] [PATCH] " Dmitry Kozlyuk
2020-02-17 23:56 ` Dmitry Kozlyuk [this message]
2020-02-18 21:52   ` [dpdk-dev] [PATCH v2] " Ranjit Menon
2020-02-21 16:37     ` Thomas Monjalon

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200217235616.85746-1-dmitry.kozliuk@gmail.com \
    --to=dmitry.kozliuk@gmail.com \
    --cc=dev@dpdk.org \
    --cc=harini.ramakrishnan@microsoft.com \
    --cc=ocardona@microsoft.com \
    --cc=pallavi.kadam@intel.com \
    --cc=ranjit.menon@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).