Hallo,
I have the problem with the parsing of the http_proxy variable (see thread
Now, in order to follow some best practices (at least that is what I have read), I need to use malloc before the usage of char * variables. Now, my variables are:
Code:
char *prxHostname, *envProxySrv, *envSproxySrv;
and I retrieve them as:
Code:
if (getenv("http_proxy")){
envProxySrv=malloc(sizeof getenv("http_proxy")+1);
strcpy(envProxySrv, getenv("http_proxy"));
}
if (getenv("https_proxy")){
envSproxySrv=malloc(sizeof getenv("https_proxy")+1);
strcpy(envSproxySrv, getenv("https_proxy"));
}
It compiles just fine, but it crashes AFTER the execution of the aforementioned statements with the following error:
Code:
*** glibc detected *** ./programname: free(): invalid next size (fast): 0x08054a70 ***
======= Backtrace: =========
/lib/libc.so.6(+0x6dffb)[0xb7606ffb]
/lib/libc.so.6(+0x6ed7b)[0xb7607d7b]
/lib/libc.so.6(cfree+0x6d)[0xb760badd]
/lib/libc.so.6(+0x28f08)[0xb75c1f08]
/lib/libc.so.6(+0x266dc)[0xb75bf6dc]
/lib/libc.so.6(+0x25fff)[0xb75befff]
/lib/libc.so.6(dcgettext+0x43)[0xb75bde53]
/lib/libc.so.6(__strerror_r+0x5b)[0xb760f38b]
/lib/libc.so.6(strerror+0x3a)[0xb760f28a]
/lib/libcrypto.so.1.0.0(ERR_load_ERR_strings+0x12e)[0xb74565de]
/lib/libcrypto.so.1.0.0(ERR_load_crypto_strings+0x14)[0xb7457604]
/lib/libssl.so.1.0.0(SSL_load_error_strings+0x14)[0xb757b154]
/usr/lib/libneon.so.27(+0x1bcc6)[0xb776acc6]
/usr/lib/libneon.so.27(ne_sock_init+0x78)[0xb775f928]
Naturally, I run the program in debug mode and I noticed that the prxProxySrv is assigned at the memory block 0x8054430
From the netbeans memory monitor, I set that address and 64 bytes as the length and I have the following:
Code:
0x08054430 68 74 74 70 3A 2F 2F 31 30 2E 31 2E 33 2E 39 32 http://10.1.3.92
0x08054440 3A 33 31 32 38 00 00 00 00 00 00 00 00 00 00 00 :3128...........
0x08054450 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x08054460 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
I was expecting the malloc of prsSproxySrv to be on the next free block, that of the "0x08054450"
However, when the second malloc is executed, I have the following memory output:
Next, I noticed that the following allocation:
Code:
0x08054430 68 74 74 70 3A 2F 2F 31 30 2E 31 2E 11 00 00 00 http://10.1.....
0x08054440 3A 33 31 32 38 00 00 00 00 00 00 00 21 2E 39 32 :3128.......!.92
0x08054450 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0x08054460 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
So, with that mess, I find it completely natural to have issues with free() in an unknown location/library.
So, what I am I doing wrong?