From nobody  Wed Mar  5 21:57:52 1997
Received: (from nobody@localhost)
          by freefall.freebsd.org (8.8.5/8.8.5) id VAA15455;
          Wed, 5 Mar 1997 21:57:52 -0800 (PST)
Message-Id: <199703060557.VAA15455@freefall.freebsd.org>
Date: Wed, 5 Mar 1997 21:57:52 -0800 (PST)
From: pius@iago.ienet.com
To: freebsd-gnats-submit@freebsd.org
Subject: pkg_delete segfaults when +CONTENTS doesn't set a prefix using @cwd
X-Send-Pr-Version: www-1.0

>Number:         2893
>Category:       bin
>Synopsis:       pkg_delete segfaults when +CONTENTS doesn't set a prefix using @cwd
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    jkh
>State:          closed
>Quarter:
>Keywords:
>Date-Required:
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Wed Mar  5 22:00:02 PST 1997
>Closed-Date:    Thu Mar 6 02:22:07 PST 1997
>Last-Modified:  Thu Mar  6 02:22:33 PST 1997
>Originator:     Pius Fischer
>Release:        3.0-CURRENT
>Organization:
>Environment:
FreeBSD iago.ienet.com 3.0-CURRENT FreeBSD 3.0-CURRENT #0: Wed Mar  5
19:20:16 PST 1997     pius@iago.ienet.com:/usr/src/sys/compile/IAGO  i386

>Description:
Sorry to be so picky, this is a very small problem, but I encountered it
when I wanted to remove the MH package (I wanted to do a fresh install of
the MH port), but realized that for some reason there was no directory
/var/db/pkg/mh-6.8.4/. So I just created it, copied the PLIST from the port
to +CONTENTS, and typed "pkg_delete -v mh-6.8.4" which resulted in a coredump!

The problem: +CONTENTS didn't contain the "@cwd /usr/local" because that
gets added when you install the port or the package, but I had just copied
the PLIST straight from the port. So pkg_delete segfaults because in perform.c
it ends up doing a setenv(PKG_PREFIX_VNAME, NULL, 1) and setenv dereferences
the NULL pointer! The correct behavior should probably be either to set the
environment variable to "" or to print a warning message.
>How-To-Repeat:
Well, here's what I did:

mkdir /var/db/pkg/mh-6.8.4
cd /var/db/pkg/mh-6.8.4
cp /usr/ports/mail/mh/pkg/PLIST +CONTENTS
cd
pkg_delete -v mh-6.8.4

>Fix:
Well, perhaps one of these two patches should be applied:

(1)

--- perform.c.orig      Wed Mar  5 21:19:30 1997
+++ perform.c   Wed Mar  5 21:53:51 1997
@@ -94,7 +94,7 @@
     read_plist(&Plist, cfile);
     fclose(cfile);
     setenv(PKG_PREFIX_VNAME,
-          (p = find_plist(&Plist, PLIST_CWD)) ? p->name : NULL, 1);
+          (p = find_plist(&Plist, PLIST_CWD)) ? p->name : "", 1);
     if (fexists(REQUIRE_FNAME)) {
        if (Verbose)
            printf("Executing 'require' script.\n");

(2)

--- perform.c.orig      Wed Mar  5 21:19:30 1997
+++ perform.c   Wed Mar  5 21:54:46 1997
@@ -93,8 +93,12 @@
        add_plist(&Plist, PLIST_CWD, Prefix);
     read_plist(&Plist, cfile);
     fclose(cfile);
-    setenv(PKG_PREFIX_VNAME,
-          (p = find_plist(&Plist, PLIST_CWD)) ? p->name : NULL, 1);
+    p = find_plist(&Plist, PLIST_CWD);
+    if (!p) {
+       whinge("Package '%s' doesn't have a prefix.", pkg);
+       return 1;
+    }
+    setenv(PKG_PREFIX_VNAME, p->name, 1);
     if (fexists(REQUIRE_FNAME)) {
        if (Verbose)
            printf("Executing 'require' script.\n");

>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->jkh 
Responsible-Changed-By: asami 
Responsible-Changed-When: Wed Mar 5 22:52:42 PST 1997 
Responsible-Changed-Why:  
While Jordan's not here, assign all pkg_* bugs to him. 

From: Pius Fischer <pius@iago.ienet.com>
To: freebsd-gnats-submit@freebsd.org
Cc:  Subject: Re: bin/2893: pkg_delete segfaults when +CONTENTS doesn't set a prefix using @cwd 
Date: Thu, 06 Mar 1997 01:13:06 -0800

 Forgive me for generating more traffic on this, but now that I
 think about it, perhaps the default prefix should be "/usr/local"
 instead of "" as suggested below.
 
 > -          (p = find_plist(&Plist, PLIST_CWD)) ? p->name : NULL, 1);
 > +          (p = find_plist(&Plist, PLIST_CWD)) ? p->name : "", 1);
 
 ... or perhaps there should be a more descriptive error message,
 e.g. "Package '%s' not deleted: a path prefix is not defined.
       Please use @cwd in +CONTENTS or -p on the command-line."
 
 > -    setenv(PKG_PREFIX_VNAME,
 > -          (p = find_plist(&Plist, PLIST_CWD)) ? p->name : NULL, 1);
 > +    p = find_plist(&Plist, PLIST_CWD);
 > +    if (!p) {
 > +       whinge("Package '%s' doesn't have a prefix.", pkg);
 > +       return 1;
 > +    }
 > +    setenv(PKG_PREFIX_VNAME, p->name, 1);
 
 Btw. I hope this is the right way to issue a follow-up on a PR.
 I didn't want to send mail from Netscape because I was afraid it
 would send an HTMLized message.
 
 Regards,
 Pius
State-Changed-From-To: open->closed 
State-Changed-By: jkh 
State-Changed-When: Thu Mar 6 02:22:07 PST 1997 
State-Changed-Why:  
2nd suggested fix applied; thanks! 
>Unformatted:
