473,799 Members | 3,390 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

corrupted double-linked list

Hello everyone. I have a problem with my program... and i kinda dunno what to do.. everything seems to work ok, but i'm getting
corrupted double-linked list error =\.
*** glibc detected *** corrupted double-linked list: 0x080aff40 ***

Program received signal SIGABRT, Aborted.
0xaf7037b2 in _dl_sysinfo_int 80 () from /lib/ld-linux.so.2
(gdb) bt
#0 0xaf7037b2 in _dl_sysinfo_int 80 () from /lib/ld-linux.so.2
#1 0xaf5b4f41 in raise () from /lib/tls/libc.so.6
#2 0xaf5b66e7 in abort () from /lib/tls/libc.so.6
#3 0xaf5e857e in __fsetlocking () from /lib/tls/libc.so.6
#4 0xaf5f2297 in mallopt () from /lib/tls/libc.so.6
#5 0xaf5f110d in mallopt () from /lib/tls/libc.so.6
#6 0xaf5efcbb in free () from /lib/tls/libc.so.6
#7 0x0804a191 in parse (buff=0x805d3f8 "$MyINFO $ALL [AA:027]Burry <++ V:0.688,M:A,H:1/0/0,S:10>$ $0.005\001$$476 05417905$|", fd=8, conn=0xa) at main.c:803
#8 0x0804b970 in main () at main.c:948
(gdb) x/s 0x805d3f8
0x805d3f8: "$MyINFO $ALL [AA:027]Burry <++ V:0.688,M:A,H:1/0/0,S:10>$ $0.005\001$$476 05417905$|"

int
parse(char *buff, int fd, PGconn *conn)
{

char **xp2,
*temp,
*tag,
*temp2,
*co,
*nick,
*ip,
*query;
PGresult *res;
int x,
i,
total,
total2;
float share;
FILE *fp;

nick = (char *) malloc(150);
ip = (char *) malloc(150);
tag = (char *) malloc(150);
query = (char *) malloc(150);

temp = (char *) malloc(2048);
temp2 = (char *) malloc(2048);
co = (char *) malloc(2048);

if (strstr(buff, "MyINFO") != NULL && strlen(buff) 30) {
strncpy(nick, buff + 12,
strlen(buff + 12) -
strlen(strstr(b uff + 15, " ")));
// fprintf(stderr, "Nick:%s\n" , nick);
xp2 = explode(buff, "$", &total2);
if (total2 >= 6) {
if (strstr(xp2[2], "M:") != NULL)
strncpy(tag, strstr(xp2[2], "M:") + 2, 1);
share = atof(xp2[6]);
if (strlen(strstr( nick, " ") + 1) 49)
return 1;
strcpy(nick, strstr(nick, " ") + 1);
fprintf(stderr,
"Nick %s jest w trybie %s i udostepnia %.f (%s) bajtow.\n",
nick, tag, share, xp2[6]);
checkshare(nick , tag, share, fd);
}

checkip(nick, fd);

memset(tag, 0, 30);
memset(nick, 0, 30);
} else if (strstr(buff, "Bad nickname") != NULL) {
fprintf(stderr, "[%d]%s\n", total, buff);
exit(0);
} else
fprintf(stderr, "[%d]%s\n", total, buff);
mfree(temp);
//printf("po2\n") ;fflush(0);
mfree(tag);
//printf("po3\n") ;fflush(0);
mfree(temp2);
//printf("po4\n") ;fflush(0);
mfree(co);
//printf("po5\n") ;fflush(0);
mfree(nick);
//printf("po6\n") ;fflush(0);
mfree(ip);
//printf("po7\n") ;fflush(0);
mfree(query);

return 0;
}
I think the problem is with explode function, but i used it in other programs and had no problems.
char *
substr(char *src, const int start, const int count)
{
char *tmp;
tmp = (char *) malloc(count + 1);
if (tmp == NULL) {
// libcgi_error(E_ MEMORY, "%s, line %s", __FILE__, __LINE__);

return NULL;
}

strncpy(tmp, src + start, count);
tmp[count] = '\0';

return tmp;
}

char **explode(char *src, const char *token, int *total)
{
char **str;
register int i, j, count, item, start;
int len;

if (!src || !token) {
*total = 0;

return NULL;
}

count = item = start = 0;
j = 1;
len = strlen(src);

// Scans the string to verify how many pieces we heave
for (i = 0; i < len; i++) {
if (src[i] == *token)
count++;
}

// We don't have any piece to explode. Returning...
if (count == 0) {
*total = 0;

return NULL;
}

// Allocate memory for the structure ( count lines )
str = (char **)malloc(count * sizeof(char *));
if (str == NULL)
return NULL;
//libcgi_error(E_ MEMORY, "%s, line %s", __FILE__, __LINE__);

// Now we'll going to get each piece and store it in the structure
for (i = 0; i < len; i++) {
if (src[i] != *token)
j++;
else {
// Found one. Now we need to allocate memory to store data
str[item] = (char *)malloc(j-start);
if (str[item] == NULL) {
// libcgi_error(E_ MEMORY, "%s, line %s", __FILE__, __LINE__);

return NULL;
}

*(str+item) = substr(src, start, j-1);

str[item][j-start-1] = '\0';
item++;
start = j++;
}
}

// The last one
*(str+count) = (char *)malloc(j);
if (str[count] == NULL)
return NULL;
//libcgi_error(E_ MEMORY, "%s, line %s", __FILE__, __LINE__);

str[count] = substr(src, start, j);
str[count][j-start] = '\0';
*total = ++count;

return str;
}

It is part of libcgi library which is free.
Basiclly i'm stuck =\
I'm trying to write a nice DC++ bot and this is used to parse messages from server.
Thing is that program crashes very often so it's kinda useless =\

I would really apricciate any help!

And sorry for my poor english =\

--
Best regards,
Oskar.

Oct 10 '06 #1
6 38229

oskar wrote:
Hello everyone. I have a problem with my program... and i kinda dunno what to do.. everything seems to work ok, but i'm getting
corrupted double-linked list error =\.
In which case, it doesn't "work ok" - does it?

I've not analysed your code in detail, but I've quickly found one
buffer overrun, and I wouldn't like to swear that you don't have
more...
char **explode(char *src, const char *token, int *total)
{
[snip]
>
// Allocate memory for the structure ( count lines )
str = (char **)malloc(count * sizeof(char *));
So you've allocated room for "count" pointers... str to (str + count
-1) or str[0] to str[count -1] - right?
// The last one
*(str+count) = (char *)malloc(j);
if (str[count] == NULL)
return NULL;
What's wrong with this picture?

Oct 10 '06 #2

You have a lot of risky string manipulation you're doing there!

All those strncpy's and strcpys need protection so you don't overrun
the size of the buffers.

In most of the strncpy's, you're not using the length parameter to
avoid buffer overflow, you're using it to copy a certain, usually
unchecked substring.

Oct 10 '06 #3
ma**********@po box.com wrote:
oskar wrote:
<snip>
> // Allocate memory for the structure ( count lines )
str = (char **)malloc(count * sizeof(char *));

So you've allocated room for "count" pointers... str to (str + count
-1) or str[0] to str[count -1] - right?
<snip>

In addition the cast is not required. If the compiler complains without
it then that just means you have done something else wrong such as not
including stdlib.h or using a C++ compiler instead of a C compiler. A
far better (less error prone) form for the malloc is
str = malloc(count * sizeof *str);

Also, to the OP, please don't use tabs when posting to Usenet. Sometimes
they get stripped from posts loosing all of the formatting.
--
Flash Gordon
Oct 10 '06 #4
Flash Gordon <sp**@flash-gordon.me.ukwro te:
Also, to the OP, please don't use tabs when posting to Usenet. Sometimes
they get stripped from posts loosing all of the formatting.
Indeed the format, which may also manifest in other newsreaders as
absurdly over-indented code, is verily "loosed" upon all they that hath
not the same newsreader as OP, at which there be rarely much
rejoicing.

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gma il.com | don't, I need to know. Flames welcome.
Oct 10 '06 #5
Christopher Benson-Manica wrote:
Flash Gordon <sp**@flash-gordon.me.ukwro te:
>Also, to the OP, please don't use tabs when posting to Usenet. Sometimes
they get stripped from posts loosing all of the formatting.

Indeed the format, which may also manifest in other newsreaders as
absurdly over-indented code, is verily "loosed" upon all they that hath
not the same newsreader as OP, at which there be rarely much
rejoicing.
I've just loosed my mind, but don't worry, it's 'armless. :-)

Getting back to the topic, its loosed because the link to the rest of me
has been corrupted. Fortunately using a debugger I can see what has gone
wrong so I'll just patch the pointer for now and correct the off-by-one
error later.
--
Flash Gordon
Dyslexic software developer,
At least the compiler ensures I spell variable names consistently wrong.
Oct 10 '06 #6
On Tue, 10 Oct 2006 14:04:51 +0000 (UTC), oskar <os***@sense.do .pl>
wrote:
>Hello everyone. I have a problem with my program... and i kinda dunno what to do.. everything seems to work ok, but i'm getting
corrupted double-linked list error =\.

snip system specific debug info
>int
parse(char *buff, int fd, PGconn *conn)
{

char **xp2,
*temp,
*tag,
*temp2,
*co,
*nick,
*ip,
*query;
PGresult *res;
int x,
i,
total,
total2;
float share;
FILE *fp;

nick = (char *) malloc(150);
ip = (char *) malloc(150);
tag = (char *) malloc(150);
query = (char *) malloc(150);

temp = (char *) malloc(2048);
temp2 = (char *) malloc(2048);
co = (char *) malloc(2048);
It would be prudent to get rid of the casts and check that these
succeeded.
>
if (strstr(buff, "MyINFO") != NULL && strlen(buff) 30) {
strncpy(nick, buff + 12,
strlen(buff + 12) -
strlen(strstr(b uff + 15, " ")));
What happens if there is no space at or after buff+15?
> // fprintf(stderr, "Nick:%s\n" , nick);
xp2 = explode(buff, "$", &total2);
There is no prototype in scope for explode.
> if (total2 >= 6) {
There are paths through explode which don't set total2.
> if (strstr(xp2[2], "M:") != NULL)
strncpy(tag, strstr(xp2[2], "M:") + 2, 1);
share = atof(xp2[6]);
if (strlen(strstr( nick, " ") + 1) 49)
return 1;
strcpy(nick, strstr(nick, " ") + 1);
fprintf(stderr,
"Nick %s jest w trybie %s i udostepnia %.f (%s) bajtow.\n",
nick, tag, share, xp2[6]);
checkshare(nick , tag, share, fd);
}

checkip(nick, fd);

memset(tag, 0, 30);
memset(nick, 0, 30);
} else if (strstr(buff, "Bad nickname") != NULL) {
fprintf(stderr, "[%d]%s\n", total, buff);
exit(0);
} else
fprintf(stderr, "[%d]%s\n", total, buff);
mfree(temp);
//printf("po2\n") ;fflush(0);
mfree(tag);
//printf("po3\n") ;fflush(0);
mfree(temp2);
//printf("po4\n") ;fflush(0);
mfree(co);
//printf("po5\n") ;fflush(0);
mfree(nick);
//printf("po6\n") ;fflush(0);
mfree(ip);
//printf("po7\n") ;fflush(0);
mfree(query);

return 0;
}
I think the problem is with explode function, but i used it in other programs and had no problems.
Unfortunately, one of the more obnoxious forms of undefined behavior
is to appear to work as expected.
>

char *
substr(char *src, const int start, const int count)
{
char *tmp;
tmp = (char *) malloc(count + 1);
if (tmp == NULL) {
// libcgi_error(E_ MEMORY, "%s, line %s", __FILE__, __LINE__);

return NULL;
}

strncpy(tmp, src + start, count);
tmp[count] = '\0';

return tmp;
}

char **explode(char *src, const char *token, int *total)
{
char **str;
register int i, j, count, item, start;
int len;

if (!src || !token) {
*total = 0;

return NULL;
}

count = item = start = 0;
j = 1;
len = strlen(src);

// Scans the string to verify how many pieces we heave
for (i = 0; i < len; i++) {
if (src[i] == *token)
count++;
}

// We don't have any piece to explode. Returning...
if (count == 0) {
*total = 0;

return NULL;
}

// Allocate memory for the structure ( count lines )
str = (char **)malloc(count * sizeof(char *));
if (str == NULL)
return NULL;
//libcgi_error(E_ MEMORY, "%s, line %s", __FILE__, __LINE__);

// Now we'll going to get each piece and store it in the structure
for (i = 0; i < len; i++) {
if (src[i] != *token)
j++;
else {
// Found one. Now we need to allocate memory to store data
str[item] = (char *)malloc(j-start);
if (str[item] == NULL) {
// libcgi_error(E_ MEMORY, "%s, line %s", __FILE__, __LINE__);

return NULL;
}

*(str+item) = substr(src, start, j-1);
This causes a memory leak. *(str+item) is identical to str[item]. You
just allocated space and stored the address returned by malloc in
str[item]. Here you replace that address with the one returned by
substr.
>
str[item][j-start-1] = '\0';
item++;
start = j++;
}
}

// The last one
Unfortunately, you are past the last element of str.
> *(str+count) = (char *)malloc(j);
This invokes undefined behavior. str points to an area capable of
holding count char*. The indices of these pointers range from 0 to
count-1. str[count] does not exist and attempting to store a value in
it is illegal.
> if (str[count] == NULL)
return NULL;
//libcgi_error(E_ MEMORY, "%s, line %s", __FILE__, __LINE__);

str[count] = substr(src, start, j);
str[count][j-start] = '\0';
*total = ++count;

return str;
}

It is part of libcgi library which is free.
Basiclly i'm stuck =\
I'm trying to write a nice DC++ bot and this is used to parse messages from server.
Thing is that program crashes very often so it's kinda useless =\

I would really apricciate any help!

And sorry for my poor english =\

Remove del for email
Oct 11 '06 #7

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

Similar topics

3
2233
by: r.e.s. | last post by:
No matter which site I download it from, NumTut.tgz appears to be corrupted. Anyone else having this problem? Thanks. -- r.e.s.
0
1811
by: Anup Jishnu | last post by:
Hi, I have installed ASP.Net application on a system. When accessing the Application from within the LAN, it works fine. However, when I access the application from the Internet, some pages gve the following error for VIEWSTATE. The internal network is protected by a Firewall. My suspicion is that
0
1605
by: Vincent Zhu | last post by:
I moved ASP WebPages from Windows 2000 server to Windows 2003 server platform. Those double-byte characters (Japanese) retrieved from SQL server 2000 are corrupted. I am using Macromedia Dreamweaver MX as the development tool, by encoding webpage as "UTF-8" as the following, <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> I also add <%
2
2956
by: Nate | last post by:
Hello, I am trying to recover a SQL Server 7 database from another hard disk drive that has a corrupted Windows 2000 Advanced Server installation. I am not able to repair the corrupted Windows 2000 Advanced Server installation but the file system is intact. I have installed a new copy of SQL Server 7 onto a new hard disk and have used the sp_attach_db system stored procedure to attach the database from the old hard drive into the new...
28
4065
by: Lee Rouse | last post by:
Hello all, This is going to be a rather lengthy "question". I have an Access 2k database, separated front end/back end. Front end copies are on about 30 workstations and used frequently during the work day. The backend has a table called CLIENTS with approximately 6000 client records. Changes to data in the table are made via a frontend db Form which has CLIENTS as its record source.
6
1538
by: Lee | last post by:
Thanks for the many responses to my original posting on 2/13 in which I asked for help about record corruptions which were occuring in a Access 2k table. After reading, printing and reading again all these postings, it was clear that the first thing I needed to do was double check that all computers had the latest version of msjet40, and that all Office updates were applied. After rechecking computers, I found several that did not have...
1
1649
by: @ndre | last post by:
Any type declaration of double leads to a TypeLoadException when run in cf (no compilation error), i.e. cf = reduced c# set??? see example below comments? @ndre this compiles with cf, but give an TypeLoadException: public class test_TypeLoadException { static void Main() {
9
2393
by: advance scout | last post by:
HELP! My database is suddenly corrupted. My computer was acting funny (very sluggish) and was shut down. Access had been already been closed down but computer was acting very slow , so perhaps it was still open. I am now getting a "The data base ... is corrupted and needs to be repaired" error . I have made copies of the corrupted database so that I could try to repair and still have a corrupted copy available. When I try to repair , it...
4
2700
by: catherineod | last post by:
Hi, could anyone help me with this problem? I'm getting the following error message when I try to run this code - "Run-Time Check Failure #2 - Stack around the variable 'vY' was corrupted" Where am I going wrong? (I'm using visual C++) #include <iostream> // Needed for cin, cout, cerr, endl #include <cmath> // Needed for Sin/Cos functions
18
399
by: brekehan | last post by:
I ran across some code that is initializing a double: double x = 1e-5; Does the standard dictate the compiler to interpret that as 0.00001? or is the above representation error prone? , Chris
0
9543
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
10488
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...
0
10257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10237
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
10029
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
9077
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6808
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
5588
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3761
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.