This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
[patch] Handle 0 result from sscanf when parsing fp values.
- From: dje at google dot com (Doug Evans)
- To: gdb-patches at sourceware dot org
- Date: Mon, 16 Aug 2010 17:31:13 -0700 (PDT)
- Subject: [patch] Handle 0 result from sscanf when parsing fp values.
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
+}