public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: dje@google.com (Doug Evans)
To: gdb-patches@sourceware.org
Subject: [patch] Handle 0 result from sscanf when parsing fp values.
Date: Tue, 17 Aug 2010 00:31:00 -0000	[thread overview]
Message-ID: <20100817003114.087EA84B8F@ruffy.mtv.corp.google.com> (raw)

Hi.

I was getting an internal error from "p 0x1.1".

(gdb) p 0x1.1
gdb/gdbtypes.c:1385: internal-error: check_typedef: Assertion `type' failed.
A problem internal to GDB has been detected,
further debugging may prove unreliable.

This is due to sscanf returning 0 and c-exp.y:parse_number not handling it.
NOTE: Not all glibc's trigger this, and you have to do "p 0x1.1" first,
otherwise the global yylval (c_lval) variable will just get reused,
and no crash (or error!).

I will commit the following in two days if there are no objections.

NOTE: This patch uses gdb_assert_not_reached.
ref: http://sourceware.org/ml/gdb-patches/2010-08/msg00250.html


2010-08-16  Doug Evans  <dje@google.com>

	* c-exp.y (parse_number): Handle 0 result from sscanf.
	* objc-exp.y (parse_number): Ditto.

	testsuite/
	* gdb.base/printcmds.exp (test_float_literals_rejected): New proc.
	Call it to test handling of bad floating point numbers.
	* gdb.objc/printcmds.exp: New file.

Index: c-exp.y
===================================================================
RCS file: /cvs/src/src/gdb/c-exp.y,v
retrieving revision 1.76
diff -u -p -u -p -r1.76 c-exp.y
--- c-exp.y	28 Jun 2010 20:18:26 -0000	1.76
+++ c-exp.y	17 Aug 2010 00:17:29 -0000
@@ -1371,12 +1371,18 @@ parse_number (char *p, int len, int pars
 		    &putithere->typed_val_float.dval, s);
       p[len] = saved_char;	/* restore the input stream */
 
-      if (num == 1)
-	putithere->typed_val_float.type = 
-	  parse_type->builtin_double;
-
-      if (num == 2 )
+      switch (num)
 	{
+	case 0:
+	  free (s);
+	  return ERROR;
+
+	case 1:
+	  putithere->typed_val_float.type = 
+	    parse_type->builtin_double;
+	  break;
+
+	case 2:
 	  /* See if it has any float suffix: 'f' for float, 'l' for long 
 	     double.  */
 	  if (!strcasecmp (s, "f"))
@@ -1390,6 +1396,10 @@ parse_number (char *p, int len, int pars
 	      free (s);
 	      return ERROR;
 	    }
+	  break;
+
+	default:
+	  gdb_assert_not_reached ("unexpected sscanf result");
 	}
 
       free (s);
Index: objc-exp.y
===================================================================
RCS file: /cvs/src/src/gdb/objc-exp.y,v
retrieving revision 1.38
diff -u -p -u -p -r1.38 objc-exp.y
--- objc-exp.y	5 Mar 2010 20:18:14 -0000	1.38
+++ objc-exp.y	17 Aug 2010 00:17:29 -0000
@@ -1016,8 +1016,9 @@ parse_number (p, len, parsed_float, puti
 
       /* It's a float since it contains a point or an exponent.  */
 
-      sscanf (p, "%" DOUBLEST_SCAN_FORMAT "%c",
-	      &putithere->typed_val_float.dval, &c);
+      if (sscanf (p, "%" DOUBLEST_SCAN_FORMAT "%c",
+		  &putithere->typed_val_float.dval, &c) != 1)
+	return ERROR;
 
       /* See if it has `f' or `l' suffix (float or long double).  */
 
Index: testsuite/gdb.base/printcmds.exp
===================================================================
RCS file: /cvs/src/src/gdb/testsuite/gdb.base/printcmds.exp,v
retrieving revision 1.35
diff -u -p -u -p -r1.35 printcmds.exp
--- testsuite/gdb.base/printcmds.exp	21 Jul 2010 18:08:27 -0000	1.35
+++ testsuite/gdb.base/printcmds.exp	17 Aug 2010 00:17:29 -0000
@@ -146,6 +146,10 @@ proc test_integer_literals_rejected {} {
     test_print_reject "p 0b12" 
 }
 
+proc test_float_literals_rejected {} {
+    test_print_reject "p 0x1.1"
+}
+
 proc test_print_all_chars {} {
     global gdb_prompt
 
@@ -794,6 +798,7 @@ if [set_lang_c] then {
     if [runto_main] then {
 	test_integer_literals_accepted
 	test_integer_literals_rejected
+	test_float_literals_rejected
 	test_character_literals_accepted
 	test_print_all_chars
 	test_print_repeats_10
Index: testsuite/gdb.objc/printcmds.exp
===================================================================
RCS file: testsuite/gdb.objc/printcmds.exp
diff -N testsuite/gdb.objc/printcmds.exp
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ testsuite/gdb.objc/printcmds.exp	17 Aug 2010 00:17:29 -0000
@@ -0,0 +1,53 @@
+# This testcase is part of GDB, the GNU debugger.
+
+# Copyright 2010 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# Please email any bugs, comments, and/or additions to this file to:
+# bug-gdb@gnu.org
+
+if $tracelevel {
+    strace $tracelevel
+}
+
+# Set the current language to Objective-C.  This counts as a test.  If it
+# fails, then we skip the other tests.
+
+proc set_lang_objc {} {
+    global gdb_prompt
+
+    if [gdb_test_no_output "set language objective-c" "set language objective-c"] {
+	return 0
+    }
+
+    if [gdb_test "show language" ".* source language is \"objective-c\".*"] {
+	return 0
+    }
+    return 1;
+}
+
+proc test_float_literals_rejected {} {
+    test_print_reject "p 0x1.1"
+}
+
+# Start with a fresh gdb.
+
+gdb_exit
+gdb_start
+gdb_reinitialize_dir $srcdir/$subdir
+
+if [set_lang_objc] {
+    test_float_literals_rejected
+}

             reply	other threads:[~2010-08-17  0:31 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-08-17  0:31 Doug Evans [this message]
2010-08-17  8:18 ` Andreas Schwab
2010-08-17 11:37   ` Doug Evans
2010-08-18 20:38     ` Doug Evans
2010-08-20  7:34       ` [patch] Fix new FAIL `reject p 0x1.1' [Re: [patch] Handle 0 result from sscanf when parsing fp values.] Jan Kratochvil
2010-08-20  7:37         ` [patch] Fix new FAIL `reject p 0x1.1' [fixup] " Jan Kratochvil
2010-08-20 11:58           ` Joseph S. Myers
2010-08-23 15:35             ` Doug Evans
2010-08-23 18:55               ` [patch] Fix new FAIL `reject p 0x1.1' [fixup] Jan Kratochvil
2010-08-23 19:49                 ` Doug Evans
2010-08-23 19:57                   ` Doug Evans
2010-08-31 19:38                     ` Jan Kratochvil
2010-08-31 22:51                       ` Doug Evans
2010-09-02 15:11                         ` Jan Kratochvil
2010-09-06 22:29                           ` Jan Kratochvil

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=20100817003114.087EA84B8F@ruffy.mtv.corp.google.com \
    --to=dje@google.com \
    --cc=gdb-patches@sourceware.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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).