linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Martin Buchan <M.J.Buchan@gre.ac.uk>
To: linux-c-programming@vger.kernel.org
Cc: Glynn Clements <glynn.clements@virgin.net>
Subject: Re: segfault with strdup
Date: Wed, 21 May 2003 10:50:20 +0100	[thread overview]
Message-ID: <20030521095020.GF674@gre.ac.uk> (raw)
In-Reply-To: <16074.22695.413401.35248@cerise.nosuchdomain.co.uk>

On Tue, May 20, 2003 at 05:32:39PM +0100, Glynn Clements wrote:
> 
> 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);

This highlights my fundamental misunderstanding of memory management
in C :-( (I come from lightweight langs like perl where i dont need
to worry about this stuff)

However, this has given me a better grasp of where i am going wrong
and now i realise that i was very lucky to get the prog to work at
all in the first place as I am doing this kind of thing all over the
place.

I tried as you suggested and it cured that problem but has given
birth to more problems of the same ilk i think. Now i know where i
am going wrong though so hopefully i can fix them. (Although i
havent yet :-)

Thanks again

Martin

      reply	other threads:[~2003-05-21  9:50 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
2003-05-21  9:50   ` Martin Buchan [this message]

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=20030521095020.GF674@gre.ac.uk \
    --to=m.j.buchan@gre.ac.uk \
    --cc=glynn.clements@virgin.net \
    --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).