Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Segfault caused by cpptcl interfering with USE_TCL_STUBS #10

Open
mutability opened this issue Jul 31, 2018 · 2 comments
Open

Segfault caused by cpptcl interfering with USE_TCL_STUBS #10

mutability opened this issue Jul 31, 2018 · 2 comments

Comments

@mutability
Copy link

This program crashes with a segfault; it tries to call Tcl_CreateInterp via the tcl stubs pointer, but the stubs pointer is null. (case 1)

#include "cpptcl.h"
#include "tcl.h"

int main(int argc, char **argv) {
  Tcl_CreateInterp();
  return 0;
}

This program does not crash (case 2)

#include "tcl.h"

int main(int argc, char **argv) {
  Tcl_CreateInterp();
  return 0;
}

This program also does not crash (case 3)

#include "tcl.h"
#include "cpptcl.h"

int main(int argc, char **argv) {
  Tcl_CreateInterp();
  return 0;
}

The cause is that cpptcl.h is defining USE_TCL_STUBS before including tcl.h
This is inappropriate for the case where cpptcl is being used from a program that's just embedding tcl, rather than an extension to be embedded in an existing interpreter.

Whether it actually breaks things is very dependent on the order of includes; if tcl.h is included before cpptcl.h, then cpptcl.h's USE_TCL_STUBS has no effect and everything is fine.

example2.cc in the cpptcl tree is an example of case 3. Swapping the order of the includes produces case 1, which then crashes.

@AndruePeters
Copy link

Has this been addressed?

@snoe925
Copy link
Contributor

snoe925 commented Dec 19, 2019

We failed to get a function added to Tcl which could create an interpreter regardless of the compiler macros implemented by USE_TCL_STUBS. Currently you have to create the TCL interpreter from a compilation unit without USE_TCL_STUBS defined. When USE_TCL_STUBS is defined Tcl_CreateInterp is a macro which guarantees a crash.

See https://core.tcl-lang.org/tips/doc/trunk/tip/531.md which contains an example of the C code needed to create an interpreter.

The spirit of the TIP 531 code is that one should be able to use USE_TCL_STUBS all the time. The only time you need to avoid the C macros USE_TCL_STUBS creates is when creating the first Tcl interpreter instance. That's why we are trying to get the function included with Tcl.

If you are not creating a Tcl extension, then you can avoid the use of USE_TCL_STUBS by defining CPPTCL_NO_TCL_STUBS. Note that the Tcl TEA framework for extensions always sets USE_TCL_STUBS in those build configurations. Therefore, the default is for an extension author not an embedded Tcl author.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants