[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

argz_stringify



Hi,

I was trying to use argv_stringify on a `near' argv structure; it has
everything but the null terminator, for example,

  char test[] = "a\0b\0c\0d\0e".

If I make the call as follows:
argz_stringify(test, sizeof(test), ' ');

This works great, (ie when the last character is indeed a null).  However, if
it happens that the last character is not a null, for example:

  argz_stringify(test, 9, ' ');

the function ignores the specified limit and it appears to continue until a
SIGSEGV is sent; this happens even if the character immediately following the
last one is a null.  The following code demonstrates what happens:

#include <argz.h>

int main(void)
{
  char test[] = "a\0b\0c\0d\0e\0\0\0\0\0\0\0";
  //             1 23 45 67 89
  argz_stringify(test, 9, ' ');

  printf("%s\n", test);

  return 0;
}


neal@hurd:~ $ gcc -g test.c
neal@hurd:~ $ gdb a.out
GNU gdb 19990928
Copyright 1998 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386-unknown-gnu0.2"...
(gdb) run
Starting program: /home/neal/a.out
[Switching to thread 668.3]
[Switching to thread 668.4]

Program received signal SIGBUS, Bus error.
0x10ab282 in argz_stringify () from /lib/libc.so.0.2
(gdb) bt
#0  0x10ab282 in argz_stringify () from /lib/libc.so.0.2
#1  0x8048549 in main () at test.c:7
(gdb) up
#1  0x8048549 in main () at test.c:7
7         argz_stringify(test, 9, ' ');
(gdb) p test
$1 = "a b c d e        "

It also breaks when test is:
char test[] = "a\0b\0c\0d\0efghijk";

neal@hurd:~ $ gcc -g test.c
neal@hurd:~ $ gdb a.out
GNU gdb 19990928
Copyright 1998 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386-unknown-gnu0.2"...
(gdb) run
Starting program: /home/neal/a.out
[Switching to thread 695.3]
[Switching to thread 695.4]

Program received signal SIGBUS, Bus error.
0x10ab282 in argz_stringify () from /lib/libc.so.0.2
(gdb) bt
#0  0x10ab282 in argz_stringify () from /lib/libc.so.0.2
#1  0x8048552 in main () at test.c:7
(gdb) up
#1  0x8048552 in main () at test.c:7
7         argz_stringify(test, 9, ' ');
(gdb) p test
$1 = "a b c d efghijk "
(gdb) quit

-Neal

--
----------------------------------------------------------------------------
Neal Walfield                                              neal@walfield.org
UMass Lowell - Fox 1512                                  Phone: 978-934-5347
                                                           Fax: 603-415-3645
Love is the triumph of imagination over intelligence.
                -- H. L. Mencken


Reply to: