aboutsummaryrefslogtreecommitdiff
path: root/gdb/target.c
diff options
context:
space:
mode:
authorPedro Alves <palves@redhat.com>2013-03-11 12:22:20 +0000
committerPedro Alves <palves@redhat.com>2013-03-11 12:22:20 +0000
commit39086a0e1346572fac07542ede2a1ef153308a2a (patch)
tree55287581f85b93530d40c06578f98c07ba535cac /gdb/target.c
parent905851752e89f4fa2cc13e6df0f2dbd22c82f623 (diff)
downloadgdb-39086a0e1346572fac07542ede2a1ef153308a2a.zip
gdb-39086a0e1346572fac07542ede2a1ef153308a2a.tar.gz
gdb-39086a0e1346572fac07542ede2a1ef153308a2a.tar.bz2
Avoid invalid pointer to pointer conversions.
Casts between 'char **' <-> 'unsigned char **' and 'char **' <-> const char **' are actually invalid: http://gcc.gnu.org/ml/gcc-help/2013-03/msg00118.html In a nutshell, char (and variants) can alias anything, but pointers to chars get no special treatment (cf. C99/N1256, 6.5/7). Turns out older gcc's actually warn/complain on these constructs, though newer one's don't: http://sourceware.org/ml/gdb-patches/2013-03/msg00429.html http://sourceware.org/ml/gdb-patches/2013-03/msg00430.html This patch fixes the cases I added last week. It also fixes one other preexisting case in charset.c, though it seems even older gccs don't complain of char * <-> const char * aliasing. Tested on x86_64 Fedora 17. gdb/ 2013-03-11 Pedro Alves <palves@redhat.com> * charset.c (convert_between_encodings): Don't cast between different pointer to pointer types. Instead, make the 'inp' local be of the type iconv expects. (wchar_iterate): Don't cast between different pointer to pointer types. Instead, use new pointer local of the type iconv expects. * target.c (target_read_stralloc, target_fileio_read_stralloc): Add new local of type char pointer, and use it to get a char/string view of the byte buffer, instead of casting between pointer to pointer types.
Diffstat (limited to 'gdb/target.c')
-rw-r--r--gdb/target.c30
1 files changed, 16 insertions, 14 deletions
diff --git a/gdb/target.c b/gdb/target.c
index 0329da3..a961d7b 100644
--- a/gdb/target.c
+++ b/gdb/target.c
@@ -2378,11 +2378,12 @@ char *
target_read_stralloc (struct target_ops *ops, enum target_object object,
const char *annex)
{
- char *buffer;
+ gdb_byte *buffer;
+ char *bufstr;
LONGEST i, transferred;
- transferred = target_read_alloc_1 (ops, object, annex,
- (gdb_byte **) &buffer, 1);
+ transferred = target_read_alloc_1 (ops, object, annex, &buffer, 1);
+ bufstr = (char *) buffer;
if (transferred < 0)
return NULL;
@@ -2390,11 +2391,11 @@ target_read_stralloc (struct target_ops *ops, enum target_object object,
if (transferred == 0)
return xstrdup ("");
- buffer[transferred] = 0;
+ bufstr[transferred] = 0;
/* Check for embedded NUL bytes; but allow trailing NULs. */
- for (i = strlen (buffer); i < transferred; i++)
- if (buffer[i] != 0)
+ for (i = strlen (bufstr); i < transferred; i++)
+ if (bufstr[i] != 0)
{
warning (_("target object %d, annex %s, "
"contained unexpected null characters"),
@@ -2402,7 +2403,7 @@ target_read_stralloc (struct target_ops *ops, enum target_object object,
break;
}
- return buffer;
+ return bufstr;
}
/* Memory transfer methods. */
@@ -3542,11 +3543,12 @@ target_fileio_read_alloc (const char *filename, gdb_byte **buf_p)
char *
target_fileio_read_stralloc (const char *filename)
{
- char *buffer;
+ gdb_byte *buffer;
+ char *bufstr;
LONGEST i, transferred;
- transferred = target_fileio_read_alloc_1 (filename,
- (gdb_byte **) &buffer, 1);
+ transferred = target_fileio_read_alloc_1 (filename, &buffer, 1);
+ bufstr = (char *) buffer;
if (transferred < 0)
return NULL;
@@ -3554,11 +3556,11 @@ target_fileio_read_stralloc (const char *filename)
if (transferred == 0)
return xstrdup ("");
- buffer[transferred] = 0;
+ bufstr[transferred] = 0;
/* Check for embedded NUL bytes; but allow trailing NULs. */
- for (i = strlen (buffer); i < transferred; i++)
- if (buffer[i] != 0)
+ for (i = strlen (bufstr); i < transferred; i++)
+ if (bufstr[i] != 0)
{
warning (_("target file %s "
"contained unexpected null characters"),
@@ -3566,7 +3568,7 @@ target_fileio_read_stralloc (const char *filename)
break;
}
- return buffer;
+ return bufstr;
}