[Openmcl-devel] popen is not happy

Gary Byers gb at clozure.com
Wed Jun 4 14:46:51 PDT 2003



On Wed, 4 Jun 2003, Erann Gat wrote:

>
> > I'm less convinced that it's safe to pass stack-allocated strings to
> > #_popen; whether this works or not may depend on whether the forked
> > child is able to use them before the parent deallocates them.  It
> > looks very much like the exception you got here was in the child
> > process; typing an "X" at the prompt killed the child process, and
> > the pipe-based FILE* was returned to the parent process.
>
> That's a good theory, but there are two problems.  First, you'd have the
> same problem if you wrote the following C code:
>
> {
>   char * cmd = "ls";
>   char * mode = "r+";
>   return popen(cmd, mode);
> }
>

The analogous C code would be something closer to:

{
   char cmd[2+1];
   char mode[2+1];

   strcpy(cmd, "ls");	/* I may have strcpy's args backwards here */
   strcpy(mode, "r+");  /* but I hope the example is clear. *./
   return popen(cmd, mode);
}

The stack addresses of 'cmd' and 'mode' might get scribbled on before
popen() is really through with them (it's probably done with 'mode',
but has to pass 'cmd' to some sort of #_exec system call.)

> and if that were going to be a problem I would think there's be a pretty
> big warning in the popen man page.
>
> The second problem is that the same thing happens with heap-allocated
> cstrs:
>
> ? (setf s1 (CCL::%NEW-PTR 4 NIL))
> #<A Mac Pointer #x41BC0>
> ? (setf s2 (CCL::%NEW-PTR 4 NIL))
> #<A Mac Pointer #x41A80>
> ? (ccl::%cstr-pointer "ls" s1)
> NIL
> ? (ccl::%cstr-pointer "r+" s2)
> NIL
> ? (#_popen s1 s2)
> #<A Null Mac Pointer>
> ?

One thing that didn't happen in the heap-allocated-string case was the
segfault ("unhandled exception 11 ...").

>
> Actually, this looks like a bug in OSX's popen -- or a bug in their man
> page.  Popen does not normally support bidirectional I/O, but the
> Jaguar man page says it does, but aparently it doesn't.  I get the same
> results in Python and C. Bummer.
>

A lot of the OSX C library code (and quite possibly the man pages) came
from FreeBSD.  The FreeBSD popen man page and the OSX man page seem to
be identical.  The FreeBSD kernel and the OSX kernel are not identical:
FreeBSD supports bidirectional pipes and OSX doesn't.

> With a unidirectional popen and heap-allocated cstrs it works.
>
> E.
>
>
> _______________________________________________
> Openmcl-devel mailing list
> Openmcl-devel at clozure.com
> http://clozure.com/cgi-bin/mailman/listinfo/openmcl-devel
>
>

_______________________________________________
Openmcl-devel mailing list
Openmcl-devel at clozure.com
http://clozure.com/cgi-bin/mailman/listinfo/openmcl-devel



More information about the Openmcl-devel mailing list