Usenet comp.sys.dec (1990-2013)
 help / color / mirror / Atom feed
From: Ron Natalie <ron@sensor.com>
Subject: Re: Segmentation Violation
Date: 1996/11/06	[thread overview]
Message-ID: <3280E47C.B0E@sensor.com> (raw)
In-Reply-To: 55qgbj$tdi@pulp.ucs.ualberta.ca


Nader Behzad wrote:
>   char *tempstring;
> 
>   tempstring = (char *)Malloc(256*sizeof(char));
> 

I don't know what "Malloc" is, but if it is the same as
"malloc" there is no guarantee as to the contents.

Also, I'm not clear why you bother to set tempstring
to anything here as you just clobber it two lines
later (leaking 256 bytes in the process).  Also,
"sizeof (char)" is ALWAYS 1 (since sizeof is defined
to return things in units of char's).

>   char_count = strlen(angle_file);
>   tempstring = strrchr(angle_file, '/');
>   if (tempstring != NULL) {
>      tempstring++;
>   }
> 
>   i =  (char_count - strlen(tempstring)); /*  <-------- A  */
>   strncpy(temp, angle_file, i);
>   temp[i] = '\0';
> 
> "tempstring" is NULL and this causes the program to crash at point A.
> The strlen result for NULL should be 0 right??

No it should not be.  strlen(NULL) is illegal.  There is a difference
between a pointer to a zero-length string and a pointer value of zero.
Strlen is not guaranteed to do anything if the argument is not a pointer
to a null terminated. string.

> I don't have this problem under SUN_OS or Solaris platforms.

You're extremely lucky.  Some platforms encourage people to write non
portable and bogus code such as this by putting a zero at location zero,
but you can not and should not rely on this behavior.  Getting a
violation
or possibly an arbitrary value return from strlen are also possible.

-Ron



  reply	other threads:[~1996-11-06  5:00 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-11-06  5:00 Segmentation Violation Nader Behzad
1996-11-06  5:00 ` Ron Natalie [this message]
1996-11-06  5:00 ` Steve Strange USG
1996-11-06  5:00   ` Ron Natalie
read-only archive

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox