public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Chen Gang <gang.chen@asianux.com>
To: linux@horizon.com, Jiri Kosina <jkosina@suse.cz>,
	andriy.shevchenko@linux.intel.com, andrei.emeltchenko@intel.com
Cc: Andrew Morton <akpm@linux-foundation.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: [PATCH] lib/vsprintf.c: fix the incorrect return value of vsnprintf()
Date: Thu, 18 Jul 2013 14:28:49 +0800	[thread overview]
Message-ID: <51E78B21.50907@asianux.com> (raw)

When "str >= end", necessary to reset 'str' to "end - 1", or the return
value will be larger than the real one, the callers which depend on the
return value, may cause memory overflow.

When for copying constant string, 'str' is point to destination buffer,
so it need add 'copy' (for destination buffer), not 'read' (for source
buffer).

Also add white space between variable and operator to make code cleaner.

Signed-off-by: Chen Gang <gang.chen@asianux.com>
---
 lib/vsprintf.c |    8 +++++---
 1 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index 739a363..b153768 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -1542,7 +1542,7 @@ int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
 					copy = end - str;
 				memcpy(str, old_fmt, copy);
 			}
-			str += read;
+			str += copy;
 			break;
 		}
 
@@ -1662,12 +1662,14 @@ int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
 	if (size > 0) {
 		if (str < end)
 			*str = '\0';
-		else
+		else {
 			end[-1] = '\0';
+			str = end - 1;
+		}
 	}
 
 	/* the trailing null byte doesn't count towards the total */
-	return str-buf;
+	return str - buf;
 
 }
 EXPORT_SYMBOL(vsnprintf);
-- 
1.7.7.6

             reply	other threads:[~2013-07-18  6:29 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-07-18  6:28 Chen Gang [this message]
2013-07-18  6:42 ` [PATCH] lib/vsprintf.c: fix the incorrect return value of vsnprintf() George Spelvin
2013-07-18  6:50   ` Chen Gang
2013-07-18  6:49 ` Al Viro
2013-07-18  6:52   ` Chen Gang

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=51E78B21.50907@asianux.com \
    --to=gang.chen@asianux.com \
    --cc=akpm@linux-foundation.org \
    --cc=andrei.emeltchenko@intel.com \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=jkosina@suse.cz \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@horizon.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