Usenet comp.sys.dec (1990-2013)
 help / color / mirror / Atom feed
* Segmentation Violation
@ 1996-11-06  5:00 Nader Behzad
  1996-11-06  5:00 ` Ron Natalie
  1996-11-06  5:00 ` Steve Strange USG
  0 siblings, 2 replies; 4+ messages in thread
From: Nader Behzad @ 1996-11-06  5:00 UTC (permalink / raw)





Hi all,

Could anyone tell me if I am doing something wrong here or is it
the compiler:

  char *tempstring;

  tempstring = (char *)Malloc(256*sizeof(char));

  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??
I don't have this problem under SUN_OS or Solaris platforms.

Thank you,
Sincerely,
Nader



--
---------------------------------------------------------------------
|                                                                   | 
| Nader T. Behzad                                                   |
| e-mail: nbehzad@ee.ualberta.ca                                    |
| WWW: http://www.ee.ualberta.ca/~nbehzad                           |
|                                                                   |
|===================================================================|
|                                                                   |
| "Whatever you do will be insignificant, but it is very            |
|  important that you do it." --Mahatma Gandhi                      |
|                                                                   |
---------------------------------------------------------------------



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Segmentation Violation
  1996-11-06  5:00 Segmentation Violation Nader Behzad
@ 1996-11-06  5:00 ` Ron Natalie
  1996-11-06  5:00 ` Steve Strange USG
  1 sibling, 0 replies; 4+ messages in thread
From: Ron Natalie @ 1996-11-06  5:00 UTC (permalink / raw)



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



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Segmentation Violation
  1996-11-06  5:00 Segmentation Violation Nader Behzad
  1996-11-06  5:00 ` Ron Natalie
@ 1996-11-06  5:00 ` Steve Strange USG
  1996-11-06  5:00   ` Ron Natalie
  1 sibling, 1 reply; 4+ messages in thread
From: Steve Strange USG @ 1996-11-06  5:00 UTC (permalink / raw)



In article <55qgbj$tdi@pulp.ucs.ualberta.ca> nbehzad@ee.ualberta.ca (Nader Behzad) writes:

>   Hi all,
>
>   Could anyone tell me if I am doing something wrong here or is it
>   the compiler:

>     i =  (char_count - strlen(tempstring)); /*  <-------- A  */

>   "tempstring" is NULL and this causes the program to crash at point A.
>   The strlen result for NULL should be 0 right??
>   I don't have this problem under SUN_OS or Solaris platforms.

I assume you're talking about Digital UNIX here.

From the strlen(3) manpage:

                                     Note
       If you pass a NULL pointer as the const char * parameter to the strlen
       function, the function generates a segmentation violation.  To avoid
       the segmentation violation and cause the function to return 0 (zero),
       change the NULL pointer treatment for the process before issuing the
       call to the strlen function, as follows:

        1.
         Include the system header file sys/uswitch.h.

        2.
         Call the uswitch function, as described in the uswitch(3) reference
         page.

       The following program illustrates this procedure:

            #include <stdio.h>
            #include <sys/types.h>
            #include <sys/uswitch.h>

            main()
            {
                    size_t  retval;
                    int     uswitch_val;

                    uswitch_val = uswitch(USC_GET,0);
                    uswitch(USC_SET, uswitch_val | USW_NULLP);
                    retval = strlen(NULL);


Hope this helps.

	Steve
--
Steve Strange						strange@zk3.dec.com
Digital Equipment Corporation, Nashua, NH		     (603) 881-0595
Digital UNIX Filesystem Development -- DCE DFS



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Segmentation Violation
  1996-11-06  5:00 ` Steve Strange USG
@ 1996-11-06  5:00   ` Ron Natalie
  0 siblings, 0 replies; 4+ messages in thread
From: Ron Natalie @ 1996-11-06  5:00 UTC (permalink / raw)



uswitch_val = uswitch(USC_GET,0);
                 uswitch(USC_SET, uswitch_val | USW_NULLP);

Great, braindead VAX compatibility makes it to the alphas.



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~1996-11-06  5:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1996-11-06  5:00 Segmentation Violation Nader Behzad
1996-11-06  5:00 ` Ron Natalie
1996-11-06  5:00 ` Steve Strange USG
1996-11-06  5:00   ` Ron Natalie

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