Bug 191285 - lang/expect segfault in exp_spawnv
Summary: lang/expect segfault in exp_spawnv
Status: Closed Not Accepted
Alias: None
Product: Ports & Packages
Classification: Unclassified
Component: Individual Port(s) (show other bugs)
Version: Latest
Hardware: Any Any
: --- Affects Only Me
Assignee: freebsd-tcltk (Nobody)
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-06-22 19:58 UTC by Richard M Kreuter
Modified: 2014-06-26 15:11 UTC (History)
1 user (show)

See Also:


Attachments
Replace some ckalloc/ckfree calls in exp_clib.c with malloc/free. (878 bytes, patch)
2014-06-22 19:58 UTC, Richard M Kreuter
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Richard M Kreuter 2014-06-22 19:58:57 UTC
Created attachment 144040 [details]
Replace some ckalloc/ckfree calls in exp_clib.c with malloc/free.

Hello,

lang/expect 5.45_1 segfaults when C programs call exp_spawnv. This has been reported and fixed in Debian for a while:

https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=588817

Attached is the patch from Debian; it suffices to correct the problem. The Debian diff also includes a couple of sample programs that reproduce the segfault; leaving those out here, but they can be found, along with other patches, here:

http://ftp.de.debian.org/debian/pool/main/e/expect/expect_5.45-2.diff.gz

Thanks,
Richard M Kreuter
Comment 1 Mark Linimon freebsd_committer freebsd_triage 2014-06-25 02:41:29 UTC
Over to maintainers.
Comment 2 Pietro Cerutti freebsd_committer freebsd_triage 2014-06-25 08:30:55 UTC
I don't think it's related to ckalloc / ckfree. The problem is the definition of Tcl_ErrnoMsg:

#define Tcl_ErrnoMsg (tclStubsPtr->tcl_ErrnoMsg)

I fear you don't have a properly setup interp.

Please try this sample program:

#include <sys/types.h>
#include <unistd.h>
#include <tcl.h>
#include <expect.h>
#include <expect_tcl.h>

int main(void)
{
    Tcl_Interp * interp = Tcl_CreateInterp();
    Expect_Init(interp);

    char * date = "/bin/date";
    char * args[] = {date, NULL};

    int fd = exp_spawnv(date, args);

    char buf[64];
    read(fd, buf, sizeof(buf));
    puts(buf);
    close(fd);

    return (0);
}

Compile with something like this:

cc -o test-spawn test-spawn.c -I/usr/local/include/tcl8.6 -I/usr/local/include -L/usr/local/lib -lexpect -ltcl86

It works as expected here. However, if you remove the assignment to interp and just leave the variable uninitialized or NULL-initialized, it will segfault exactly as you described.
Comment 3 Richard M Kreuter 2014-06-26 15:02:46 UTC
The libexpect manual explicitly says that libexpect can be used directly from C or C++, without the use of Tcl, and there's no mention of needing to initialize a Tcl interpreter from C. (If that's now considered a deprecated/unsupported way to use libexpect, I guess it's just a documentation issue.)
Comment 4 Pietro Cerutti freebsd_committer freebsd_triage 2014-06-26 15:11:46 UTC
libexpect(3) starts with 

       #include expect_tcl.h
       Expect_Init(interp);

       cc files... -lexpect5.20 -ltcl7.5 -lm

so you see that somehow tcl is included and linked against.

I think they assume that interp is a correctly initialized interpreter.

I agree that the documentation could be improved to assume less and explain more. Please report this upstream.

Thank you,