Subject: bin/2609: /bin/sh segfaults if 'cd -' is called before cd'ing
To: None <gnats-bugs@NetBSD.ORG>
From: None <jim@santafe.arch.columbia.edu>
List: netbsd-bugs
Date: 07/08/1996 05:39:51
>Number:         2609
>Category:       bin
>Synopsis:       /bin/sh segfaults if 'cd -' is called before cd'ing
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    bin-bug-people (Utility Bug People)
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Mon Jul  8 05:50:02 1996
>Last-Modified:
>Originator:     Jim Wise
>Organization:
	Columbia University Digital Design Lab
>Release:        1.1
>Environment:
	NetBSD/1.1 i386, but not platform specific
System: NetBSD erebor 1.1 NetBSD 1.1 (EREBOR) #2: Sun Jun 30 03:52:37 EDT 1996 root@erebor:/usr/share/src/sys/arch/i386/compile/EREBOR i386


>Description:
	Due to an uninitialized variable in /usr/src/bin/sh/cd.c, calling
	'cd -' before cd has been called with no argument or another
	argument causes a null pointer to be dereferenced.
>How-To-Repeat:
	$ /bin/sh
	$ cd -
	Segmentation fault - core dumped
>Fix:
	
Submitted 7/8/96 by Jim Wise (jim@santafe.arch.columbia.edu)

This patch fixes a bug in /bin/sh whereby executing 'cd -' to change to a
previous directory causes a core dump if cd has not been called yet.

*** cd.c.orig	Mon Jul  8 04:50:00 1996
--- cd.c	Mon Jul  8 05:11:00 1996
***************
*** 68,79 ****
  
  STATIC int docd __P((char *, int));
  STATIC char *getcomponent __P((void));
! STATIC void updatepwd __P((char *));
  STATIC void getpwd __P((void));
  
  char *curdir;			/* current working directory */
  char *prevdir;			/* previous working directory */
  STATIC char *cdcomppath;
  
  int
  cdcmd(argc, argv)
--- 68,96 ----
  
  STATIC int docd __P((char *, int));
  STATIC char *getcomponent __P((void));
! MKINIT STATIC void updatepwd __P((char *));
  STATIC void getpwd __P((void));
  
  char *curdir;			/* current working directory */
  char *prevdir;			/* previous working directory */
  STATIC char *cdcomppath;
+ 
+ #ifdef mkinit
+ 
+ INCLUDE	<unistd.h>
+ 
+ INIT {
+ 	updatepwd(getcwd(NULL, 0));
+ }
+ 
+ RESET {
+ 	updatepwd(getcwd(NULL, 0));
+ }
+ 
+ SHELLPROC {
+ 	updatepwd(getcwd(NULL, 0));
+ }
+ #endif
  
  int
  cdcmd(argc, argv)
>Audit-Trail:
>Unformatted: