Skip to content
Tcl Source Code View Ticket
Login

View Ticket

Win32 potential crash when using main()

Closed / Fixedd402ffe76f90c650bcf18d5b5c58a0b77770ed48Created 7 years agoUpdated 21 days ago
Descriptionplain2020-05-10 20:13:51
According to MSDN the _wenviron variable initially is NULL when
the program starts via main() but is populated on the very first
call of _wgetenv() or _wputenv().

The current core-8-6-branch requires in TclSetupEnv() _wenviron
to be non NULL, otherwise a crash (access violation, segfault)
happens. When some custom code embeds Tcl as a library and uses
main() instead of wmain() as its program entry point, that crash
is most likely.

Please see https://www.androwish.org/home/info/93ee54fe8cd2e176
for a possible solution.
Referenced by4
jan.nijtmanswiki2020-05-10 20:58:59

Thanks! I will have a look at this.

jan.nijtmanswiki2020-05-12 11:29:13

Merged to core-8-6-branch and up. Thanks for the report and the fix!

anonymous (claiming to be [email protected])plain2026-11-10 18:10:43
This issue still exists in 9.0 due to TclFinalizeIOSubsystem() calling TclGetEnv() around line 598 of tclIO.c.

It's possible for trivial apps to initialise and finalise and hit this path. I encountered this creating a minimal repro for a build issue with CPython. 

The fix I'd suggest is to have TclpFindVariable (in tclWinInit.c) check for a NULL _wenviron and do an arbitrary _wgetenv to initialise before continuing. I assume there's some reason for doing your own search rather than using _wgetenv (or _wgetenv_s), but if not, switching to that for the search ought to be fine too.
oehhar Open ticket [d402ffe76f]. [History diff]2026-11-10 19:27:25
jan.nijtmanswiki2026-11-10 20:02:21

Something like [this]?

jan.nijtmanswiki2026-11-11 13:56:12

No merged to [trunk], core-9-0-branch and core-8-6-branch

jan.nijtmanswiki2026-11-11 14:10:58

Now merged to [trunk], core-9-0-branch and core-8-6-branch

Log in (anonymous is fine) to add a comment.