mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Paul <set@pobox.com>
To: linux-kernel@vger.kernel.org
Subject: Re: [PATCH] Fix sscanf (the 3rd verse)
Date: Mon, 24 Sep 2001 03:13:20 -0400	[thread overview]
Message-ID: <20010924031320.X16708@squish.home.loc> (raw)
In-Reply-To: <200109240354.WAA03909@ccure.karaya.com>
In-Reply-To: <200109240354.WAA03909@ccure.karaya.com>; from jdike@karaya.com on Sun, Sep 23, 2001 at 10:54:10PM -0500

Jeff Dike <jdike@karaya.com>, on Sun Sep 23, 2001 [10:54:10 PM] said:
> sscanf double-increments fmt in a couple of places, causing format characters
> to be skipped.  Patch follows.
> 
> I'm a bit unhappy about the second chunk, but I don't see a cleaner way to
> do it offhand.
> 
> 				Jeff
> 


	Hi.

	I found buffer overruns and other problems when I looked
at this function again in more detail. (%c and %s format parsing
went horribly wrong in initial tests.)  This patch tries to fix
those also. (and avoids the increment/decrement ugly) I have
tested it a little in a userspace program, and it passes uml's
sscanf usage:) Should check it again in the morning... it may not
be perfect, but at least I hope this makes it a little safer.

Paul
set@pobox.com

--- 2.4.9-ac13-user/lib/vsprintf.c.old	Fri Sep 21 19:42:25 2001
+++ 2.4.9-ac13-user/lib/vsprintf.c	Mon Sep 24 02:53:03 2001
@@ -508,6 +508,7 @@
  * @fmt:	format of buffer
  * @args:	arguments
  */
+
 int vsscanf(const char * buf, const char * fmt, va_list args)
 {
 	const char *str = buf;
@@ -515,36 +516,37 @@
 	int num = 0;
 	int qualifier;
 	int base;
-	unsigned int field_width;
+	int field_width = -1;
 	int is_sign = 0;
 
-	for (; *fmt; fmt++) {
+	while(*fmt && *str) {
 		/* skip any white space in format */
-		if (isspace(*fmt)) {
-			continue;
-		}
+		while (isspace(*fmt))
+			++fmt;
 
 		/* anything that is not a conversion must match exactly */
-		if (*fmt != '%') {
+		if (*fmt != '%' && *fmt) {
 			if (*fmt++ != *str++)
 				return num;
 			continue;
 		}
-		++fmt;
+		if (*fmt)
+			++fmt;
+		else
+			return num;
 		
 		/* skip this conversion.
 		 * advance both strings to next white space
 		 */
 		if (*fmt == '*') {
-			while (!isspace(*fmt))
+			while (!isspace(*fmt) && *fmt)
 				fmt++;
-			while(!isspace(*str))
+			while (!isspace(*str) && *str)
 				str++;
 			continue;
 		}
 
 		/* get field width */
-		field_width = 0xffffffffUL;
 		if (isdigit(*fmt))
 			field_width = skip_atoi(&fmt);
 
@@ -557,25 +559,32 @@
 		base = 10;
 		is_sign = 0;
 
-		switch(*fmt) {
+		if (!*fmt || !*str)
+			return num;
+
+		switch(*fmt++) {
 		case 'c':
 		{
 			char *s = (char *) va_arg(args,char*);
+			if (field_width == -1)
+				field_width = 1;
 			do {
 				*s++ = *str++;
-			} while(field_width-- > 0);
+			} while(field_width-- > 0 && *str);
 			num++;
 		}
 		continue;
 		case 's':
 		{
 			char *s = (char *) va_arg(args, char *);
+			if(field_width == -1)
+				field_width = 0x7ffffff;
 			/* first, skip leading white space in buffer */
 			while (isspace(*str))
 				str++;
 
 			/* now copy until next white space */
-			while (!isspace(*str) && field_width--) {
+			while (*str && !isspace(*str) && field_width--) {
 				*s++ = *str++;
 			}
 			*s = '\0';
@@ -617,6 +626,9 @@
 		while (isspace(*str))
 			str++;
 
+		if (!*str)
+			return num;
+
 		switch(qualifier) {
 		case 'h':
 			if (is_sign) {

  reply	other threads:[~2001-09-24  7:14 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-09-24  3:54 [PATCH] Fix sscanf Jeff Dike
2001-09-24  7:13 ` Paul [this message]
2001-09-24 18:05   ` [PATCH] Fix sscanf (more fixes) Paul

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=20010924031320.X16708@squish.home.loc \
    --to=set@pobox.com \
    --cc=linux-kernel@vger.kernel.org \
    /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

all inboxes | Powered by JetHome®