V
vlindos
I have this code
<code>
#include <stdio.h>
#include <stdlib.h>
#include <mysql/mysql.h>
char locale_loaded[2];
int i18n_num;
struct i18n_t{
char *msg;
char *gsm;
} **i18n_cache;
void i18n_init(char locale[2])
{
char qbuf[1024];
MYSQL_ROW row;
MYSQL_RES *res;
strcpy(locale_loaded, locale);
sprintf(qbuf, "select msg,%s from i18n", locale);
if(mysql_query(mysql_sock, qbuf)) {
fprintf(stderr, "FATAL ERROR: Cannot connect MySQL
DB.\n");
exit(0);
}
res = mysql_store_result(mysql_sock);
i18n_num = 0;
while((row = mysql_fetch_row(res)) != NULL) {
i18n_cache = (struct i18n_t **) realloc(i18n_cache,
++i18n_num * (sizeof(struct i18n_t)));
i18n_cache[i18n_num-1] = (struct i18n_t
*)malloc(sizeof(struct i18n_t));
if(row[0] == NULL) { i18n_num--; continue; }
i18n_cache[i18n_num-1]->msg = (char
*)malloc(sizeof(row[0]));
strcpy(i18n_cache[i18n_num-1]->msg, row[0]);
if(row[1] == NULL) continue;
i18n_cache[i18n_num-1]->gsm = (char
*)malloc(sizeof(row[1]));
strcpy(i18n_cache[i18n_num-1]->msg, row[1]);
}
return;
}
</code>
It parses sereval times results and gives the unexpected:
<gdb-output>
Breakpoint 1, main () at rebmin.c:31
31 mysql_init(&mysql_t);
(gdb) s 30
31 while((row = mysql_fetch_row(res)) != NULL) {
(gdb) s
32 i18n_cache = (struct i18n_t **)
realloc(i18n_cache, ++i18n_num * (sizeof(struct i18n_t)));
(gdb) s
Program received signal SIGSEGV, Segmentation fault.
0x4207b7fd in chunk_realloc () from /lib/i686/libc.so.6
(gdb)
</gdb-output>
any ideas ?
<code>
#include <stdio.h>
#include <stdlib.h>
#include <mysql/mysql.h>
char locale_loaded[2];
int i18n_num;
struct i18n_t{
char *msg;
char *gsm;
} **i18n_cache;
void i18n_init(char locale[2])
{
char qbuf[1024];
MYSQL_ROW row;
MYSQL_RES *res;
strcpy(locale_loaded, locale);
sprintf(qbuf, "select msg,%s from i18n", locale);
if(mysql_query(mysql_sock, qbuf)) {
fprintf(stderr, "FATAL ERROR: Cannot connect MySQL
DB.\n");
exit(0);
}
res = mysql_store_result(mysql_sock);
i18n_num = 0;
while((row = mysql_fetch_row(res)) != NULL) {
i18n_cache = (struct i18n_t **) realloc(i18n_cache,
++i18n_num * (sizeof(struct i18n_t)));
i18n_cache[i18n_num-1] = (struct i18n_t
*)malloc(sizeof(struct i18n_t));
if(row[0] == NULL) { i18n_num--; continue; }
i18n_cache[i18n_num-1]->msg = (char
*)malloc(sizeof(row[0]));
strcpy(i18n_cache[i18n_num-1]->msg, row[0]);
if(row[1] == NULL) continue;
i18n_cache[i18n_num-1]->gsm = (char
*)malloc(sizeof(row[1]));
strcpy(i18n_cache[i18n_num-1]->msg, row[1]);
}
return;
}
</code>
It parses sereval times results and gives the unexpected:
<gdb-output>
Breakpoint 1, main () at rebmin.c:31
31 mysql_init(&mysql_t);
(gdb) s 30
31 while((row = mysql_fetch_row(res)) != NULL) {
(gdb) s
32 i18n_cache = (struct i18n_t **)
realloc(i18n_cache, ++i18n_num * (sizeof(struct i18n_t)));
(gdb) s
Program received signal SIGSEGV, Segmentation fault.
0x4207b7fd in chunk_realloc () from /lib/i686/libc.so.6
(gdb)
</gdb-output>
any ideas ?