Fix segfaults if Tcl is built with stubs and Expect is used directly from C program

This commit is contained in:
Vitezslav Crhonek 2014-04-28 13:47:44 +02:00
parent 7005ff5330
commit b6737eed55
2 changed files with 80 additions and 1 deletions

View File

@ -0,0 +1,71 @@
Author: Sergei Golovan <sgolovan@debian.org>
Description: This dirty hack fixes segfaults if Tcl is built with stubs
and Expect is used directly from C program.
Bug: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=588817
Example:
#include <stdio.h>
#include <tcl8.5/expect.h>
int main()
{
FILE *pipe;
char *some_command = "uname";
char datum;
pipe = exp_popen(some_command);
if (pipe == NULL) return 1;
while ((datum = getc (pipe)) != EOF)
printf("%c",datum);
}
Example:
#include <stdio.h>
#include "expect.h"
main()
{
int fd = 0;
fd = exp_spawnl("echo", "echo", "Hello User: Whats up?", (char*) 0);
switch (exp_expectl(fd, exp_regexp, "ser:", 1, exp_end)) {
case 1: {
printf("GOT ser:\n");
break;
}
default: {
printf("DEFAULT\n");
return 1;
}
}
printf("Normal Exit\n");
return 0;
}
--- expect-5.45.orig/exp_clib.c
+++ expect-5.45/exp_clib.c
@@ -117,7 +117,11 @@
#include <stdlib.h> /* for malloc */
#endif
-#include <tcl.h>
+#define ckalloc(x) Tcl_Alloc(x)
+#define ckfree(x) Tcl_Free(x)
+extern char *Tcl_ErrnoMsg(int err);
+extern char *Tcl_Alloc(unsigned int size);
+extern void Tcl_Free(char *ptr);
#include "expect.h"
#define TclRegError exp_TclRegError
@@ -389,7 +389,7 @@
FAIL("regexp too big");
/* Allocate space. */
- r = (regexp *)ckalloc(sizeof(regexp) + (unsigned)rcstate->regsize);
+ r = (regexp *)malloc(sizeof(regexp) + (unsigned)rcstate->regsize);
if (r == NULL)
FAIL("out of space");
@@ -399,7 +399,7 @@
rcstate->regcode = r->program;
regc(MAGIC, rcstate);
if (reg(0, &flags, rcstate) == NULL) {
- ckfree ((char*) r);
+ free ((char*) r);
return(NULL);
}

View File

@ -5,7 +5,7 @@
Summary: A program-script interaction and testing utility Summary: A program-script interaction and testing utility
Name: expect Name: expect
Version: %{majorver} Version: %{majorver}
Release: 13%{?dist} Release: 14%{?dist}
License: Public Domain License: Public Domain
Group: Development/Languages Group: Development/Languages
# URL: probably more useful is http://sourceforge.net/projects/expect/ # URL: probably more useful is http://sourceforge.net/projects/expect/
@ -25,6 +25,9 @@ Patch3: expect-5.45-match-gt-numchars-segfault.patch
Patch4: expect-5.45-re-memleak.patch Patch4: expect-5.45-re-memleak.patch
# Patch5: use vsnprintf instead of vsprintf to avoid buffer overflow # Patch5: use vsnprintf instead of vsprintf to avoid buffer overflow
Patch5: expect-5.45-exp-log-buf-overflow.patch Patch5: expect-5.45-exp-log-buf-overflow.patch
# Patch6: fixes segfaults if Tcl is built with stubs and Expect is used directly
# from C program rhbz#1091060
Patch6: expect-5.45-segfault-with-stubs.patch
# examples patches # examples patches
# Patch100: changes random function # Patch100: changes random function
Patch100: expect-5.32.2-random.patch Patch100: expect-5.32.2-random.patch
@ -84,6 +87,7 @@ of expectk.
%patch3 -p1 -b .match-gt-numchars-segfault %patch3 -p1 -b .match-gt-numchars-segfault
%patch4 -p1 -b .re-memleak %patch4 -p1 -b .re-memleak
%patch5 -p1 -b .exp-log-buf-overflow %patch5 -p1 -b .exp-log-buf-overflow
%patch6 -p1 -b .segfault-with-stubs
# examples fixes # examples fixes
%patch100 -p1 -b .random %patch100 -p1 -b .random
%patch101 -p1 -b .mkpasswd-dash %patch101 -p1 -b .mkpasswd-dash
@ -173,6 +177,10 @@ rm -rf "$RPM_BUILD_ROOT"
%{_mandir}/man1/tknewsbiff.1* %{_mandir}/man1/tknewsbiff.1*
%changelog %changelog
* Mon Apr 28 2014 Vitezslav Crhonek <vcrhonek@redhat.com> - 5.45-14
- Fix segfaults if Tcl is built with stubs and Expect is used directly from C program
(patch taken from Debian project, written by Sergei Golovan)
* Thu Dec 12 2013 Vitezslav Crhonek <vcrhonek@redhat.com> - 5.45-13 * Thu Dec 12 2013 Vitezslav Crhonek <vcrhonek@redhat.com> - 5.45-13
- Use vsnprintf instead of vsprintf to avoid buffer overflow - Use vsnprintf instead of vsprintf to avoid buffer overflow
(it happens e.g. when running systemtap testsuite) (it happens e.g. when running systemtap testsuite)