This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
[patch] Fix SIGSEGV in gdb when printing ctor of non-virtual class
- From: Aleksandar Ristovski <aristovski at qnx dot com>
- To: gdb-patches at sources dot redhat dot com
- Date: Tue, 09 Sep 2008 13:27:57 -0400
- Subject: [patch] Fix SIGSEGV in gdb when printing ctor of non-virtual class
Hello,
I run into a SIGSEGV crash in gdb when printing value of a non-virtual class' constructor (I got this from IDE asking for a wrong value).
Example:
(gdb) print n.Name
where 'n' is an object of non-virtual class 'Name'.
The attached tests demonstrate the problem and propose a fix.
Thanks,
Aleksandar Ristovski
QNX Software Systems
ChangeLog:
* value.c (value_fn_field): Do not dereference if block does not
exist.
testsuite ChangeLog:
* gdb.cp/cppeval.exp: New test.
* gdb.cp/cppeval.cc: New test case to accompany cppeval.exp.
Index: gdb/value.c
===================================================================
RCS file: /cvs/src/src/gdb/value.c,v
retrieving revision 1.64
diff -u -p -r1.64 value.c
--- gdb/value.c 11 Jun 2008 19:59:09 -0000 1.64
+++ gdb/value.c 9 Sep 2008 17:17:14 -0000
@@ -1415,8 +1415,8 @@ value_field (struct value *arg1, int fie
*/
struct value *
-value_fn_field (struct value **arg1p, struct fn_field *f, int j, struct type *type,
- int offset)
+value_fn_field (struct value **arg1p, struct fn_field *f, int j,
+ struct type *type, int offset)
{
struct value *v;
struct type *ftype = TYPE_FN_FIELD_TYPE (f, j);
@@ -1440,7 +1440,16 @@ value_fn_field (struct value **arg1p, st
v = allocate_value (ftype);
if (sym)
{
- VALUE_ADDRESS (v) = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
+ /* Constructors of non-virtual classes will not have block. */
+ struct block *block = SYMBOL_BLOCK_VALUE (sym);
+
+ if (block)
+ VALUE_ADDRESS (v) = BLOCK_START (block);
+ else
+ {
+ release_value (v);
+ return NULL;
+ }
}
else
{
# This testcase is part of GDB, the GNU debugger.
# Copyright 2008 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/>.
set testfile "cppeval"
set srcfile ${testfile}.cc
set binfile ${objdir}/${subdir}/${testfile}
if { [prepare_for_testing ${testfile}.exp ${testfile} "${srcfile}" {debug c++}] } {
return -1
}
runto_main
# NOTE: the following test used to crash gdb if c is an object of a
# class that inherits from non-virtual class Name.
#
# Print base constructor name.
gdb_test_multiple "print n.Name" "print constructor of nonvirtual class" {
-re "Cannot take address of method Name.*$gdb_prompt $" {
pass "${testfile}"
}
default {
fail "${testfile}"
}
}
#include <cstdlib>
#include <iostream>
#include <string.h>
/* NOTE: We intentionally don't make the classes virtual. */
class Name
{
public:
Name(void) { myName = 0; }
~Name(void) { delete myName; }
char* myName;
};
int main(int argc, char *argv[])
{
Name n;
return EXIT_SUCCESS;
}