Usenet comp.unix.osf.osf1 (1992-2004)
 help / color / mirror / Atom feed
* Re: Dereferencing NULL pointer?
  1997-05-21  4:00 Dereferencing NULL pointer? Todd M. Saylor
@ 1997-05-21  4:00 ` Steve VanDevender
  1997-05-22  4:00   ` Todd M. Saylor
  1997-05-29  4:00 ` Craig Neth USG
  1997-05-29  4:00 ` Craig Neth USG
  2 siblings, 1 reply; 7+ messages in thread
From: Steve VanDevender @ 1997-05-21  4:00 UTC (permalink / raw)



tsaylor@ecst.csuchico.edu (Todd M. Saylor) writes:

   I am currently in the process of porting an application to Digital UNIX
   4.0b (it's never been run on an alpha before).  The app runs fine on HP/UX,
   AIX, DG/UX (motorola and Intel), AT&T/NCR.  One problem has come up on
   previous ports, that is the ability to dereference a NULL pointer.  For the
   OS's that hate this, we've always found a linker option to allow it
   (basically return a null character) - for example on DG it would be '-z
   lowzeros'.
   
   The following code demonstrates what I mean:
   
   main()
   {
   char *x;
   
   x = (char *)NULL;
   
   printf("Null string = '%s'\n", x); /* This works fine */
   printf("Null Char = '%c'\n", *x); /* This get a seg. violation */
   }
   
   Now this may be bad form, but I was wondering if anyone knows of a
   switch/option to allow it?
   
   TIA
   
   [Posted for Aaron Johnson / aaron@bi-tech.com]

Your example program is inherently wrong.  A NULL pointer in ANSI C is
(void *) 0 -- it's valid to cast 0 to any pointer type to get a NULL,
and it's also valid to cast void * to any other pointer type; while
"#define NULL ((void *) 0)" is commonly used, it is no longer necessary
to use NULL as an pointer constant when you can use 0 instead.  A ASCII
NUL character (note the different spelling) is '\0'.  You don't get a
NUL character by dereference of a NULL pointer, because dereference of a
NULL pointer is undefined -- some architectures allow it but could
return any value whatsoever, while others generate SIGSEGV if you do it,
so you should never depend on it being possible in a portable program.
You need to read a C book, and so did the authors of the program you are
porting.  In the long run you will be much better off fixing the program
to eliminate the bogus NULL references than you are trying to find a way
to make it allowable on the architectures you port the program to.  I've
been through such messes already and most such NULL reference problems
introduce many subtle bugs.

-- 
Steve VanDevender  "I ride the big iron"  stevev@efn.org
stevev@hexadecimal.uoregon.edu   http://jcomm.uoregon.edu/~stevev
"bash awk grep perl sed df du, du-du du-du,
vi troff su fsck rm * halt LART LART LART!" -- the Swedish BOFH



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

* Dereferencing NULL pointer?
@ 1997-05-21  4:00 Todd M. Saylor
  1997-05-21  4:00 ` Steve VanDevender
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Todd M. Saylor @ 1997-05-21  4:00 UTC (permalink / raw)



I am currently in the process of porting an application to Digital UNIX
4.0b (it's never been run on an alpha before).  The app runs fine on HP/UX,
AIX, DG/UX (motorola and Intel), AT&T/NCR.  One problem has come up on
previous ports, that is the ability to dereference a NULL pointer.  For the
OS's that hate this, we've always found a linker option to allow it
(basically return a null character) - for example on DG it would be '-z
lowzeros'.

The following code demonstrates what I mean:

main()
{
char *x;

x = (char *)NULL;

printf("Null string = '%s'\n", x); /* This works fine */
printf("Null Char = '%c'\n", *x); /* This get a seg. violation */
}

Now this may be bad form, but I was wondering if anyone knows of a
switch/option to allow it?

TIA

[Posted for Aaron Johnson / aaron@bi-tech.com]
-- 



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

* Re: Dereferencing NULL pointer?
  1997-05-21  4:00 ` Steve VanDevender
@ 1997-05-22  4:00   ` Todd M. Saylor
  1997-05-23  4:00     ` Dan Pop
  0 siblings, 1 reply; 7+ messages in thread
From: Todd M. Saylor @ 1997-05-22  4:00 UTC (permalink / raw)



> Your example program is inherently wrong.  A NULL pointer in ANSI C is
> (void *) 0 -- it's valid to cast 0 to any pointer type to get a NULL,
> and it's also valid to cast void * to any other pointer type; while
> "#define NULL ((void *) 0)" is commonly used, it is no longer necessary
> to use NULL as an pointer constant when you can use 0 instead.  A ASCII
> NUL character (note the different spelling) is '\0'.  You don't get a
> NUL character by dereference of a NULL pointer, because dereference of a
> NULL pointer is undefined -- some architectures allow it but could
> return any value whatsoever, while others generate SIGSEGV if you do it,
> so you should never depend on it being possible in a portable program.
> You need to read a C book, and so did the authors of the program you are
> porting.  In the long run you will be much better off fixing the program
> to eliminate the bogus NULL references than you are trying to find a way
> to make it allowable on the architectures you port the program to.  I've
> been through such messes already and most such NULL reference problems
> introduce many subtle bugs.

Correct?  Yes.
Helpful?  Not so much.

These things I already know, so I think I'll skip the advice to read a
C book.  We are porting a "million line plus" application, not just a
single program, so what we are really hoping for is some way of treating
the NULL pointer as a pointer to a NUL character.  This is the way it 
ends up working on the other platforms we've used (yes, I know the results
are technically undefined, but many platforms have linker switches or other
tricks to allow this).  Blame it on legacy code.

-- 



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

* Re: Dereferencing NULL pointer?
  1997-05-22  4:00   ` Todd M. Saylor
@ 1997-05-23  4:00     ` Dan Pop
  1997-05-28  4:00       ` Vikas Sontakke TOEM
  0 siblings, 1 reply; 7+ messages in thread
From: Dan Pop @ 1997-05-23  4:00 UTC (permalink / raw)



In <5m239e$ajk@charnel.ecst.csuchico.edu> tsaylor@ecst.csuchico.edu (Todd M. Saylor) writes:

>These things I already know, so I think I'll skip the advice to read a
>C book.  We are porting a "million line plus" application, not just a
>single program, so what we are really hoping for is some way of treating
>the NULL pointer as a pointer to a NUL character.  This is the way it 
>ends up working on the other platforms we've used (yes, I know the results
>are technically undefined, but many platforms have linker switches or other
>tricks to allow this).  Blame it on legacy code.

Dereferencing a null pointer (yeah, "null" is spelled in lower case in
this context) is a BUG.  And bugs are fixed, not worked around.  Legacy
code is not an excuse for sloppiness.

Dan
--
Dan Pop
CERN, IT Division
Email: Dan.Pop@cern.ch 
Mail:  CERN - PPE, Bat. 31 1-014, CH-1211 Geneve 23, Switzerland



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

* Re: Dereferencing NULL pointer?
  1997-05-23  4:00     ` Dan Pop
@ 1997-05-28  4:00       ` Vikas Sontakke TOEM
  0 siblings, 0 replies; 7+ messages in thread
From: Vikas Sontakke TOEM @ 1997-05-28  4:00 UTC (permalink / raw)



To answer the question, take a look at 
	man uswitch
and lookup USW_NULLP

  The uswitch system call is used to get or change the compatibility environ-
  ment specific behavior in Digital UNIX.  Any changes affect the calling
  process and its children.

  When the USW_NULLP bit of uswitch is set to 1, the System V method of
  treating NULL pointers is applied.  In this method, references to a NULL
  pointer always returns zero (0).  When this bit-mask is reset to zero (0),
  subsequent references to a NULL pointer generate a segmentation violation
  signal (SIGSEGV).

  Any write(2) references to NULL pointers generate a segmentation violation
  signal (SIGSEGV) regardless of the uswitch value.

--
Vikas Sontakke	sontakke@zk3.dec.com DTN 381-0811  (603)881-0811
OEM Business Group (formerly known as E&RT), Nashua NH
_______________________________________________________________________________



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

* Re: Dereferencing NULL pointer?
  1997-05-21  4:00 Dereferencing NULL pointer? Todd M. Saylor
  1997-05-21  4:00 ` Steve VanDevender
  1997-05-29  4:00 ` Craig Neth USG
@ 1997-05-29  4:00 ` Craig Neth USG
  2 siblings, 0 replies; 7+ messages in thread
From: Craig Neth USG @ 1997-05-29  4:00 UTC (permalink / raw)
  To: Todd M. Saylor


Todd M. Saylor wrote:
> 
> I am currently in the process of porting an application to Digital UNIX
> 4.0b (it's never been run on an alpha before).The app runs fine on HP/UX,
> AIX, DG/UX (motorola and Intel), AT&T/NCR.  One problem has come up on
> previous ports, that is the ability to dereference a NULL pointer. 

[... example deleted ...]

> Now this may be bad form, but I was wondering if anyone knows of a
> switch/option to allow it?

I'll skip the lecture and tell you how to make it work:

link with sys5 library, ala:

	# cc null.c -lsys5
        # a.out
	Null string = '(null)'
	Null Char = ''

The libsys5 library includes an init procedure which makes a syscall
that maps the page at address zero.    

-- 
--------------------------------------------------------------------------------
Craig Neth                         ! neth@zk3.dec.com
DIGITAL Equipment Corporation      ! 
110 Spit Brook Road ZKO3-3/W17     !
Nashua, NH 03062                   ! Opinions expressed herein are my own.
================================================================================



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

* Re: Dereferencing NULL pointer?
  1997-05-21  4:00 Dereferencing NULL pointer? Todd M. Saylor
  1997-05-21  4:00 ` Steve VanDevender
@ 1997-05-29  4:00 ` Craig Neth USG
  1997-05-29  4:00 ` Craig Neth USG
  2 siblings, 0 replies; 7+ messages in thread
From: Craig Neth USG @ 1997-05-29  4:00 UTC (permalink / raw)
  To: Todd M. Saylor


Todd M. Saylor wrote:
> 
> 
> Now this may be bad form, but I was wondering if anyone knows of a
> switch/option to allow it?

Also, if you don't like the linking with libsys5 option, you could 
just call the syscall that maps page 0 yourself.  See the uswitch(2) 
manpage for details.   This may be more desirable for applications 
that otherwise don't want/need the sys5 library behaviors.

-- 
--------------------------------------------------------------------------------
Craig Neth                         ! neth@zk3.dec.com
DIGITAL Equipment Corporation      ! 
110 Spit Brook Road ZKO3-3/W17     !
Nashua, NH 03062                   ! Opinions expressed herein are my own.
================================================================================



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

end of thread, other threads:[~1997-05-29  4:00 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1997-05-21  4:00 Dereferencing NULL pointer? Todd M. Saylor
1997-05-21  4:00 ` Steve VanDevender
1997-05-22  4:00   ` Todd M. Saylor
1997-05-23  4:00     ` Dan Pop
1997-05-28  4:00       ` Vikas Sontakke TOEM
1997-05-29  4:00 ` Craig Neth USG
1997-05-29  4:00 ` Craig Neth USG

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