From: Glynn Clements <glynn.clements@virgin.net>
To: Martin Buchan <M.J.Buchan@gre.ac.uk>
Cc: linux-c-programming@vger.kernel.org
Subject: Re: segfault with strdup
Date: Tue, 20 May 2003 17:32:39 +0100 [thread overview]
Message-ID: <16074.22695.413401.35248@cerise.nosuchdomain.co.uk> (raw)
In-Reply-To: <20030520112034.GZ674@gre.ac.uk>
Martin Buchan wrote:
> This is all working now, however, it only works if i run it in the
> directory that i have done all my coding in. I.e. If i compile and
> install to a different location, i get a seg fault when parsing the
> XML file. Its not that it cant find the file, it goes thru and
> parses the first entry in the file and then croaks on the 2nd
> while parsing the name of the app - which is "Netscape Navigator"
> If i shorten the name to just "Netscape", it then croaks on the 3rd
> entry which is "Lynx Text Only Browser", if i then shorten this to
> just "Lynx Browser", it then croaks on the 4th and so on and so on.
There is a problem in parseTermXpm:
144 gchar *xpmdir;
145 xpmdir = strdup(XPMDIR);
...
154 termPtr[0].termxpm = strcat(xpmdir, termPtr[0].termxpm);
The buffer which is being allocated isn't guaranteed to be any larger
than XPMDIR itself (in practice it may be slightly larger due to
padding, but you can't rely on it), so the strcat() call is corrupting
the heap.
Once you corrupt the heap, all bets are off; usually what happens is
that a later call to a heap-management function (malloc, calloc, free
etc) will segfault.
You are also doing exactly the same thing with HELPDIR.
The most obvious solution is:
gchar *xpmdir;
xpmdir = malloc(strlen(XPMDIR) + strlen(termPtr[0].termxpm) + 1);
strcpy(xpmdir, XPMDIR);
...
termPtr[0].termxpm = strcat(xpmdir, termPtr[0].termxpm);
--
Glynn Clements <glynn.clements@virgin.net>
next prev parent reply other threads:[~2003-05-20 16:32 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-05-20 11:20 segfault with strdup Martin Buchan
2003-05-20 12:25 ` Chris Nanakos
2003-05-20 12:46 ` Martin Buchan
2003-05-20 13:12 ` Martin Buchan
[not found] ` <200305201316.h4KDGUhG007842@grinch.txt.com>
2003-05-20 13:52 ` Martin Buchan
2003-05-20 16:32 ` Glynn Clements [this message]
2003-05-21 9:50 ` Martin Buchan
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=16074.22695.413401.35248@cerise.nosuchdomain.co.uk \
--to=glynn.clements@virgin.net \
--cc=M.J.Buchan@gre.ac.uk \
--cc=linux-c-programming@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).