Haru - May 29, 2013 2:52
clif.c:[code=auto:0]if( !strstr(irc_server,":") ) {
hChSys.irc = false;
ShowWarning("channels.conf : network port wasn't found in 'irc_channel_network', disabling irc channel...\n");
} else {
unsigned char d = 0, dlen = strlen(irc_server);
char server[40];
for(d = 0; d < dlen; d++) {
if(irc_server[d] == ':') {
memcpy(server, irc_server, d);
safestrncpy(hChSys.irc_server, server, 40);
memcpy(server, &irc_server[d+1], dlen);
hChSys.irc_server_port = atoi(server);
break;
}
}
}
[/code]The [font=Courier]server[40][/font] variable is uninitialized, and as such - according to the C specs - it main contain garbage (specifically, after the [font=Courier]memcpy(server, irc_server, d)[/font] instruction, the string may not be NULL-terminated), causing issues such as the following, depending on the compiler used:[code=auto:0][29/May 03:24][Status]: Done reading '60' command aliases in 'conf/atcommand.conf'.
[29/May 03:24][Status]: Done reading '4' channels in 'conf/channels.conf'.
[29/May 03:24][Error]: Unable to resolve 'irc.rizon.net^5C' (irc server), disabling irc channel...
{snip}
^C
Program received signal SIGINT, Interrupt.
Error0xb7fdf424 in __kernel_vsyscall ()
(gdb) print hChSys$1 = {colors = 0xb79f61f4, colors_name = 0xb79f6244, colors_count = 13 '\r',
local = 1 '\001', ally = 1 '\001', irc = 0 '\000', local_autojoin = 1 '\001', ally_autojoin = 1 '\001',
local_name = "map", '\000' <repeats 16 times>, ally_name = "ally", '\000' <repeats 15 times>,
irc_name = "irc", '\000' <repeats 16 times>, local_color = 3 '\003', ally_color = 5 '\005',
irc_color = 11 '\v', closing = 0 '\000', allow_user_channel_creation = 0 '\000',
irc_server = "irc.rizon.net\210!\b\020^!\b5C", '\000' <repeats 17 times>,
irc_channel = "#PRIVATE\000\000\000\000\000\000\000\000\000\000\000", irc_nick = "PRIVATE", '\000' <repeats 22 times>,
irc_nick_pw = "PRIVATE", '\000' <repeats 22 times>, irc_server_port = 6667}[/code]While Apple's llvm-gcc and clang automatically initialize such variables, gcc doesn't.
[spoiler]
For reference:[code=auto_linen:0]/** test.c **/
#include <stdio.h>
#include <string.h>
int main() {
int i;
char uninitialized[16];
char initialized[16];
memset(initialized, '\0', 16);
printf("Uninitialized: ");
for (i = 0; i < 16; i++)
printf("x ", uninitialized[i]&0xff);
printf("\n");
printf("Initialized: ");
for (i = 0; i < 16; i++)
printf("x ", initialized[i]&0xff);
printf("\n");
return 0;
}[/code][code=auto_linen:0]* haru@serenity ~ $ gcc --version | head -n 1 && gcc -o test test.c && ./test
gcc (Gentoo 4.6.3 p1.13, pie-0.5.2) 4.6.3
Uninitialized: 05 35 60 b7 e0 62 77 b7 00 00 00 00 2b 85 04 08
Initialized: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00[/code][code=auto_linen:0]* haru@ks211459 ~ $ gcc --version | head -n 1 && gcc -o test test.c && ./test
gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
Uninitialized: ff b0 f0 00 00 00 00 00 a0 06 40 00 00 00 00 00
Initialized: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00[/code][code=auto_linen:0]• haru@Freya ~ $ gcc --version | head -n 1 && gcc -o test test.c && ./test
i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)
Uninitialized: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Initialized: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00[/code][code=auto_linen:0]• haru@Freya ~ $ clang --version && clang -o test test.c && ./test
Apple LLVM version 4.2 (clang-425.0.27) (based on LLVM 3.2svn)
Target: x86_64-apple-darwin12.3.0
Thread model: posix
Uninitialized: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Initialized: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00[/code][/spoiler]