Hi, I'm trying to get the following code to work. I need it to be able to run an cgi on the router (I'm cross compiling this code originally for DG834G to DG834GT). When I run it in the manner it is supposed to be used (with -s -t -r arguments) it segfaults.
(An example of where using it causes a segfault: http://pastebin.com/m7a30dec8)
I compile with -Wall and -g and get no warnings during compile (on the computer):
The router spits out "Uncaught target signal 11 (segfaulted)" and does not give me much info so I ran it on the computer (also making it run strace too) and get this (pastebinned) :
http://pastebin.com/m39635a81
I changed the length & position from 0 to -1 (probably a 101 million reasons why I shouldn't do that but...) and it stopped segfaulting on the computer but the router still segfaulted. The router uses uclibc compared to my computer with glibc.
If I can't get the cgi written in "bash script" to work, I'll probably switch over to c++.
Also, I'm pretty much a programming n00b, hence me compiling someone else's code, so please take it easy (sorry )
Thank you.
(An example of where using it causes a segfault: http://pastebin.com/m7a30dec8)
Code:
/**
* Simple string replacement utility
* coded by exovii (2005)
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <getopt.h>
void replace(char* text, char* source, char* target, char caseinsensitive)
{
long position = 0;
char *buffer;
char *result = NULL;
long length = 0;
length = (result)?strlen(result):0;
while((buffer = strstr(&text[position], source)) != NULL)
{
length = (result)?strlen(result):0;
result = (char*)realloc(result, length + (buffer - text) - strlen(source) + strlen(target));
strncat(result, &text[position], (buffer - &text[position]));
//printf("intermediate1: %s\n", result);
strcat(result, target);
//printf("intermediate2: %s\n", result);
position = buffer - text + strlen(source);
}
result = (char*)realloc(result, length + strlen(&text[position]));
strcat(result, &text[position]);
strcpy(text, result);
free(result);
//printf("--------------------------------------------------\n");
}
void usage(char *progname)
{
printf("%s usage:\n", progname);
printf("\t-t <text>\n");
printf("\t-s <search>\n");
printf("\t-r <replacement>\n");
printf("\t-i\t case insensitive search (not implemented)\n");
}
int main(int argc, char *argv[])
{
char c;
char* text = NULL;
char* source = NULL;
char* target = NULL;
char casei = 0;
while((c = getopt(argc, argv, "t:s:r:i")) != -1)
{
switch(c)
{
case 't':
text = argv[optind-1];
break;
case 's':
source = argv[optind-1];
break;
case 'r':
target = argv[optind-1];
break;
case 'i':
casei = 1;
break;
}
}
if(optind != 7)
{
usage(argv[0]);
exit(1);
}
//printf("optind: %i\n", optind);
//printf("text: %s\n", text);
//printf("source: %s\n", source);
//printf("target: %s\n", target);
replace(text, source, target, casei);
printf("%s\n", text);
return 0;
}
I compile with -Wall and -g and get no warnings during compile (on the computer):
gcc -Wall -g -c -o replace.o replace.c
gcc -Wall -g -o replace replace.o
strip replace
The router spits out "Uncaught target signal 11 (segfaulted)" and does not give me much info so I ran it on the computer (also making it run strace too) and get this (pastebinned) :
http://pastebin.com/m39635a81
*** glibc detected *** /usr/bin/replace: realloc(): invalid next size: 0x0804a008 ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6[0xb7e97803]
/lib/tls/i686/cmov/libc.so.6(realloc+0x10b)[0xb7e9975b]
/usr/bin/replace[0x80486f3]
/usr/bin/replace[0x8048890]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe0)[0xb7e40450]
/usr/bin/replace[0x8048541]
======= Memory map: ========
08048000-08049000 r-xp 00000000 08:05 1371142 /usr/bin/replace
08049000-0804a000 rw-p 00000000 08:05 1371142 /usr/bin/replace
0804a000-0806b000 rw-p 0804a000 00:00 0 [heap]
b7d00000-b7d21000 rw-p b7d00000 00:00 0
b7d21000-b7e00000 ---p b7d21000 00:00 0
b7e1e000-b7e28000 r-xp 00000000 08:05 843882 /lib/libgcc_s.so.1
b7e28000-b7e29000 rw-p 0000a000 08:05 843882 /lib/libgcc_s.so.1
b7e29000-b7e2a000 rw-p b7e29000 00:00 0
b7e2a000-b7f73000 r-xp 00000000 08:05 44795 /lib/tls/i686/cmov/libc-2.7.so
b7f73000-b7f74000 r--p 00149000 08:05 44795 /lib/tls/i686/cmov/libc-2.7.so
b7f74000-b7f76000 rw-p 0014a000 08:05 44795 /lib/tls/i686/cmov/libc-2.7.so
b7f76000-b7f79000 rw-p b7f76000 00:00 0
b7f8b000-b7f8d000 rw-p b7f8b000 00:00 0
b7f8d000-b7f8e000 r-xp b7f8d000 00:00 0 [vdso]
b7f8e000-b7fa8000 r-xp 00000000 08:05 845370 /lib/ld-2.7.so
b7fa8000-b7faa000 rw-p 00019000 08:05 845370 /lib/ld-2.7.so
bfab9000-bfacf000 rw-p bffea000 00:00 0 [stack]
I changed the length & position from 0 to -1 (probably a 101 million reasons why I shouldn't do that but...) and it stopped segfaulting on the computer but the router still segfaulted. The router uses uclibc compared to my computer with glibc.
If I can't get the cgi written in "bash script" to work, I'll probably switch over to c++.
Also, I'm pretty much a programming n00b, hence me compiling someone else's code, so please take it easy (sorry )
Thank you.
Last edited: