473,651 Members | 3,090 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

chunk_alloc segfault

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_l oaded, 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_res ult(mysql_sock) ;
i18n_num = 0;
while((row = mysql_fetch_row (res)) != NULL) {
i18n_cache = (struct i18n_t **) realloc(i18n_ca che,
++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_cac he[i18n_num-1]->msg, row[0]);
if(row[1] == NULL) continue;
i18n_cache[i18n_num-1]->gsm = (char
*)malloc(sizeof (row[1]));
strcpy(i18n_cac he[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(&mys ql_t);
(gdb) s 30
31 while((row = mysql_fetch_row (res)) != NULL) {
(gdb) s
32 i18n_cache = (struct i18n_t **)
realloc(i18n_ca che, ++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 ?
Nov 13 '05 #1
2 3081
On Wed, 22 Oct 2003 02:42:53 -0700, vlindos wrote:
I have this code
<code>
#include <stdio.h>
You need this too:
#include <string.h>
#include <stdlib.h>
#include <mysql/mysql.h>

char locale_loaded[2];

int i18n_num;

struct i18n_t{
char *msg;
char *gsm;
} **i18n_cache;
You seem to be relying on this being initialized to a null pointer.
That's fine, but why not make it explicit?

I hypothesize that you want to build an dynamically allocated array
of pointers to struct i18n_t.

Since we might want another pointer of this type, I'll go ahead and
declare another one too.

} **i18n_cache = NULL, **resized_cache = NULL;
/* for dynamically allocated array of pointer to struct i18n_t */
void i18n_init(char locale[2])
{
char qbuf[1024];
MYSQL_ROW row;
MYSQL_RES *res;

strcpy(locale_l oaded, 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_res ult(mysql_sock) ;
i18n_num = 0;

while((row = mysql_fetch_row (res)) != NULL) {
i18n_cache = (struct i18n_t **) realloc(i18n_ca che, ++i18n_num * (sizeof(struct i18n_t)));

That's a lot going on in one line. Let's see, we don't need the cast of
realloc's return value, I'd prefer to increment i18n_num before this
line, and look at that sizeof.

You are trying to allocate an array of _pointers_ to struct i18n_t. That
is clear from the next line. So sizeof(struct i18n_t) can't be right.
We can get the correct size to allocate by finding out the size of the
kind of thing that i18n_cache points to.

Lastly, you need to check to make sure that realloc succeeded. Since
it will return NULL on failure, it's probably a bad idea to assign
its return value to i18n_cache, since you'll lose your only pointer
to your array.

++i18n_num;
resized_cache = realloc(i18n_ca che, i18n_num * sizeof *i18n_cache);
if (resized_cache == NULL) {
/* oops, realloc() failed */
break; /* exit while */
}

i18n_cache = resized_cache;
i18n_cache[i18n_num-1] = (struct i18n_t *)malloc(sizeof (struct i18n_t));
You don't need to cast the result of malloc, but you do need to
make sure that it succeeded. Also, I've changed the sizeof to
find the size of the thing we're allocating instead of the size
of its type, as above.

i18n_cache[i18n_num-1] = malloc(sizeof **i18n_cache);
if (i18n_cache[i18n_num-1] == NULL) {
/* oops, malloc() failed */
break; /* exit while */
}
if(row[0] == NULL) { i18n_num--; continue; }
Why is this line here? Presumably you know whether row[0] is a
null pointer or not right after mysql_fetch_row () is called.
If you test it at the top of the while loop, then you won't
call realloc and malloc before you actually need the space.
i18n_cache[i18n_num-1]->msg = malloc(sizeof(r ow[0]));
Whoa! Without knowing anything about MySQL, this line looks
mighty suspicious. In the next line, you are using strcpy() to
copy a string that is pointed to by row[0]. Here you are
allocating a buffer for that string, but you are only
allocating enough space for a pointer! I know this is because
MYSQL_ROW is defined in mysql/mysql.h as:

typedef char **MYSQL_ROW;

This error is probably the cause of your segmentation fault.
So you need to allocate a buffer big enough to contain the
entire string, and of course you need to check to make sure
that malloc succeeded:

i18n_cache[i18n_num-1]->msg = malloc(strlen(r ow[0])+1);
if (i18n_cache[i18n_num-1]->msg == NULL) {
/* oops, malloc() failed */
break; /* exit while */
}

strcpy(i18n_cac he[i18n_num-1]->msg, row[0]);
if(row[1] == NULL) continue;
i18n_cache[i18n_num-1]->gsm = (char *)malloc(sizeof (row[1]));
You have the exact same problem here:

i18n_cache[i18n_num-1]->gsm = malloc(strlen(r ow[1])+1);
if (i18n_cache[i18n_num-1]->msg == NULL) {
/* oops, malloc() failed */
break; /* exit while */
}
strcpy(i18n_cac he[i18n_num-1]->msg, row[1]);
Surely this is supposed to copy into ->gsm instead of ->msg?
strcpy(i18n_cac he[i18n_num-1]->gsm, row[1]); }

return;
}


Here's the code again, in full, direct from my emacs. I've
compiled it

gcc -Wall -O2 -pedantic -std=c99 -c test.c

But haven't tried to test it.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <mysql/mysql.h>

char locale_loaded[2];

int i18n_num;

MYSQL *mysql_sock;

struct i18n_t {
char *msg;
char *gsm;
} **i18n_cache = NULL, **resized_cache = NULL;

void i18n_init(char locale[2])
{
char qbuf[1024];
MYSQL_ROW row;
MYSQL_RES *res;

strcpy(locale_l oaded, locale);
sprintf(qbuf, "select msg,%s from i18n", locale);

if (mysql_query(my sql_sock, qbuf))
{
fprintf(stderr, "FATAL ERROR: Cannot connect MySQL DB.\n");
exit(0);
}

res = mysql_store_res ult(mysql_sock) ;

i18n_num = 0;

while ((row = mysql_fetch_row (res)) != NULL)
{
if (row[0] == NULL) continue;

++i18n_num;

resized_cache = realloc(i18n_ca che, i18n_num * sizeof *i18n_cache);
if (resized_cache == NULL) {
/* oops, realloc() failed */
break; /* exit while loop */
}

i18n_cache = resized_cache;

i18n_cache[i18n_num - 1] = malloc(sizeof i18n_cache);
if (i18n_cache[i18n_num-1] == NULL) {
/* oops, malloc() failed */
break; /* exit while */
}

i18n_cache[i18n_num-1]->msg = malloc(strlen(r ow[0])+1);
if (i18n_cache[i18n_num-1]->msg == NULL) {
/* oops, malloc() failed */
break; /* exit while */
}

strcpy(i18n_cac he[i18n_num - 1]->msg, row[0]);

if (row[1] == NULL) continue;

i18n_cache[i18n_num-1]->gsm = malloc(strlen(r ow[1])+1);
if (i18n_cache[i18n_num-1]->gsm == NULL) {
/* oops, malloc() failed */
break; /* exit while */
}

strcpy(i18n_cac he[i18n_num - 1]->gsm, row[1]);
}

return;
}

Nov 13 '05 #2
On 22 Oct 2003 02:42:53 -0700
vl*****@abv.bg (vlindos) wrote:
I have this code
<code>
#include <stdio.h>
#include <stdlib.h>
#include <mysql/mysql.h>
#include <string.h> /* You need it for strcpy */
char locale_loaded[2];
Are you sure you mean 2? that's space for 1 char followed by a null
terminator. If I am right this could cause a segmentation violation or
anything else. I think you want

char locale_loaded[3];
int i18n_num;

struct i18n_t{
char *msg;
char *gsm;
} **i18n_cache;

void i18n_init(char locale[2])
Again, I think it is more than 2 characters! Anyway, I would just do:

void i18n_init(char *locale)
{
char qbuf[1024];
MYSQL_ROW row;
MYSQL_RES *res;

strcpy(locale_l oaded, locale);
Not very safe. What is a string longer than 1 characters (or 2
characters after my suggested change) is passed in?
sprintf(qbuf, "select msg,%s from i18n", locale);
if(mysql_query( mysql_sock, qbuf)) {
You did not include a definition of mysql_sock in the posted code. I
assume this is a global and has been properly initialised?

Anything to do with mysql is off topic for this group.
fprintf(stderr, "FATAL ERROR: Cannot connect MySQL
DB.\n");
exit(0);
}
res = mysql_store_res ult(mysql_sock) ;
i18n_num = 0;
while((row = mysql_fetch_row (res)) != NULL) {
i18n_cache = (struct i18n_t **) realloc(i18n_ca che,
++i18n_num * (sizeof(struct i18n_t)));
BAD for numerous reasons. Try

struct i18n_t **tmp_i18n_cach e =
realloc(i18n_ca che, ++i18n_num * sizeof *tmp_i18n_cache );
if (tmp_i18n_cache == NULL) {
fprintf(stderr, "FATAL ERROR: realloc i18n_cache failed\n");
exit(0);
/* You can used some error recovery if you want instead.
i18n_cache still points to the data you had */
}
i18n_cache = tmp_i18n_cache;
i18n_cache[i18n_num-1] = (struct i18n_t
*)malloc(sizeof (struct i18n_t));
Bad. Use
i18n_cache[i18n_num-1] = malloc(sizeof **i18n_cache);
if (i18n_cache[i18n_num-1] == NULL) {
fprintf(stderr,
"FATAL ERROR: malloc i18n_cache[] failed\n");
exit(0);
}

or something similar.
if(row[0] == NULL) { i18n_num--; continue; }
i18n_cache[i18n_num-1]->msg = (char
*)malloc(sizeof (row[0]));
strcpy(i18n_cac he[i18n_num-1]->msg, row[0]);
Almost certainly wrong, but MySQL is off topic here.
if(row[1] == NULL) continue;
i18n_cache[i18n_num-1]->gsm = (char
*)malloc(sizeof (row[1]));
strcpy(i18n_cac he[i18n_num-1]->msg, row[1]);
Almost certainly wrong, but MySQL is off topic here.
}

return;
}
</code>
It parses sereval times results and gives the unexpected:
<gdb-output>
Breakpoint 1, main () at rebmin.c:31
31 mysql_init(&mys ql_t);
(gdb) s 30
31 while((row = mysql_fetch_row (res)) != NULL) {
(gdb) s
32 i18n_cache = (struct i18n_t **)
realloc(i18n_ca che, ++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 ?


One of the errors above (or something else where in your code) has
probably run off the end of a buffer and overwritten something critical.
--
Mark Gordon
Paid to be a Geek & a Senior Software Developer
Although my email address says spamtrap, it is real and I read it.
Nov 13 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
3153
by: Nathaniel Echols | last post by:
I've written a function in C to perform protein sequence alignment. This works fine in a standalone C program. I've added the necessary packaging to use it in Python; it returns three strings and an integer. However, as soon as the function is complete, I get a segfault and the interpreter dies. If I run Python interactively, just calling the function causes a segfault. If I'm running a script, I can actually print out the return...
6
3015
by: Juho Saarikko | last post by:
The program attached to this message makes the Python interpreter segfault randomly. I have tried both Python 2.2 which came with Debian Stable, and self-compiled Python 2.3.3 (newest I could find on www.python.org, compiled with default options (./configure && make). I'm using the pyPgSQL plugin to connect to a PostGreSQL database, and have tried the Debian and self-compiled newest versions of that as well. I'm running BitTorrent, and...
6
1977
by: Stefan Behnel | last post by:
Hi! In Python 2.4b3, the deque is causing a segfault on two different machines I tested on. With deque, my program runs fine for a while (at least some tens of seconds up to minutes) and then suddenly segfaults. I'm sorry I can't tell exactly when, but I'm running an application that uses a few hundred deques where elements are appendleft()ed and pop()ed (simple queue). As the application is rather complex (and I didn't read about this...
0
1816
by: dale | last post by:
Python newbie disclaimer on I am running an app with Tkinter screen in one thread and command-line input in another thread using raw_input(). First question - is this legal, should it run without issue? If not can you point me to a description of why. While updating objects on the screen I get a segfault after an indeterminate number of updates. It doesn't seem to matter how quickly the updates occur, but it does segfault faster...
10
2905
by: Arthur J. O'Dwyer | last post by:
I'm seeing a bug at the moment that I can't track down. It's part of a moderately large program, but here is a small program that exhibits the bug on gcc. (The program code follows at the bottom of the message.) The symptom is this: % g++ --version g++ (GCC) 3.2.1 % g++ -W -Wall -pedantic test.cpp
4
1884
by: Jim Strathmeyer | last post by:
Under what circumstances would closing a istream object (such as 'in.close()') SEGFAULT?
4
3493
by: William Payne | last post by:
Hello, I was under the impression that if I made a class Foo and if I didn't specify a copy constructor I would get one anyway that simply assigns the member variables (and that won't work for dynamically allocated member variables). Anyway, I have a program that segfaults without a copy constructor but if I add an empty one, the segfault is gone. The code is ugly indeed so I don't want to post it, but, in general terms, what sort of error...
10
1942
by: name | last post by:
When I started testing the algorithms for my wrap program, I threw together this snippet of code, which works quite well. Except that it (predictably) segfaults at the end when it tries to go beyond the file. At some point, I tried to mend that behavior using feof() but without success. The functionality is not harmed, but this has started to bug me. What am I missing here? Sometimes being a code duffer is frustrating!! lol!!! The...
3
2299
by: kj | last post by:
I am trying to diagnose a bug in my code, but I can't understand what's going on. I've narrowed things down to this: I have a function, say foo, whose signature looks something like: int foo( int w, int x, int y, int z, my_struct **results ) During its execution, foo initializes *results using calloc: ( *results ) = calloc( w+1, sizeof( my_struct ) );
14
4951
by: Donn Ingle | last post by:
Yo, An app of mine relies on PIL. When PIL hits a certain problem font (for unknown reasons as of now) it tends to segfault and no amount of try/except will keep my wxPython app alive. My first thought is to start the app from a bash script that will check the return value of my wxPython app and could then launch a new app to help the user grok what happened and fix it. Do you think that's a good idea, or is there another way to...
0
8361
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8278
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8807
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8466
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8584
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5615
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4144
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4290
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1912
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.