Skip to content
Tcl Source Code View Ticket
Login

View Ticket

Wrong sentinel in Tcl_SetErrorCode usage

Closed / Fixed26870862f0e8884eaec02c5a3bbc4caf2431a310Created 3 years agoUpdated 3 years ago
Descriptionwiki2023-10-15 11:51:16
In tclAssembly.c, lines 2014 and 3454, the Tcl_SetErrorCode() is not properly ended with NULL. Adding compiler checks for this results in:
generic/tclAssembly.c:2014:63: warning: missing sentinel in function call [-Wsentinel]
                Tcl_SetErrorCode(interp, "TCL", "ASSEM", "DUPJUMPTABLEENTRY");
                                                                            ^
generic/tclAssembly.c:3454:64: warning: missing sentinel in function call [-Wsentinel]
            Tcl_SetErrorCode(interp, "TCL", "ASSEM", "BADSTACKINCATCH", -1);
                                                                          ^

Those two instances will lead to a crash, when encoungered. Apparently, there is no testcase for it (which would - indeed - be difficult to write)

It would be good to implement compiler-checks for this, even in 8.6

Referenced by10
jan.nijtmanswiki2023-10-15 22:03:02

Fixed [here].

Leaving open: let's add better checks

jan.nijtmanswiki2023-10-19 18:19:28

Addition: For C++-compilers, the problem is that some C++-compilers (e.g. clang-8.0.1) define "NULL" as identical to "0", which is allowed in C++-land. See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95700

The only way which should work with all C++-compilers is using (void *)NULL.

In C, this is no problem. We don't want to force extension-writers to do the same: Many extensions don't care about C++.

C++-case handled [here]

Still keeping open, it would be nice to have the compiler check for this by default.

gustafn3plain2023-10-20 07:53:09
Passing NULL after the last typed argument to a variadic function is not
recommended either, since this leads to undefined behaviour. 
This construct will lead e.g. to the following error flagged by cppcheck:

==========================================================================================
generic/tclBinary.c:403:52: portability: Passing NULL after the last 
typed argument to a variadic function leads to undefined behaviour.

The C99 standard, in section 7.15.1.1, states that if the type used 
by va_arg() is not compatible with the type of the actual next argument 
(as promoted according to the default argument promotions), the behavior is undefined.

The value of the NULL macro is an implementation-defined null pointer 
constant (7.17), which can be any integer constant expression with the value 0, 
or such an expression casted to (void*) (6.3.2.3). This includes values like 
0, 0L, or even 0LL.

In practice on common architectures, this will cause real crashes if 
sizeof(int) != sizeof(void*), and NULL is defined to 0 or any other 
null pointer constant that promotes to int.

To reproduce you might be able to use this little code example on 64bit
platforms. If the output includes "ERROR", the sentinel had only 4 out of 
8 bytes initialized to zero and was not detected as the final argument to 
stop argument processing via va_arg(). Changing the 0 to (void*)0 or 0L 
will make the "ERROR" output go away.

  Tcl_SetErrorCode(interp, "TCL", "API", "OUTDATED", NULL);
==========================================================================================

In NaviServer such occurrences have been replaced by using "(char *)0L" 
instead of "NULL" after the last typed argument. Here are some examples 
concerning Tcl_SetErrorCode():

==========================================================================================
nsd/tcljob.c:                    Tcl_SetErrorCode(interp, "NS_TIMEOUT", (char *)0L);
nsd/tcljob.c:                    Tcl_SetErrorCode(interp, jobPtr->errorCode, (char *)0L);
nsd/tcljob.c:                    Tcl_SetErrorCode(interp, "NS_TIMEOUT", (char *)0L);
nsd/tcljob.c:        Tcl_SetErrorCode(interp, "ECANCEL", (char *)0L);
nsd/tclobjv.c:        Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "INDEX", msg, key, (char *)0L);
nsd/tclvar.c:                    Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "NSV", "KEY", keyString, (char *)0L);
nsd/tclvar.c:                Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "NSV", "KEY", keyString, (char *)0L);
nsd/tclvar.c:                Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "NSV", "KEY", keyString, (char *)0L);
nsd/tclvar.c:            Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "NSV", "KEY", keyString, (char *)0L);
nsd/tclvar.c:                                    Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "DICT",
nsd/tclvar.c:        Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "NSV", "ARRAY", arrayName, (char *)0L);
nsd/tls.c:        Tcl_SetErrorCode(interp, "OPENSSL", errorBuf, (char *)0L);
nsd/urlencode.c:            Tcl_SetErrorCode(interp, "NS_INVALID_UTF8", NULL);
==========================================================================================

If there is interest, i can provide a fixed branch for such issues.
jan.nijtmansplain2023-10-20 08:14:25
@gustaf wrote:
> If there is interest, i can provide a fixed branch for such issues

See: [234ff497c44f843c]. All such occurrences of "NULL" have been replaced now by (void *)NULL, exactly for the reason you mention: NULL is non-portable in this situation.

I never saw this problem on any C-compiler, but I saw it happen in C++ with clang-8.0.1.

Hope this helps,
      Jan Nijtmans
jan.nijtmanswiki2023-10-20 08:14:48

@gustaf wrote: > If there is interest, i can provide a fixed branch for such issues

See: [234ff497c44f843c]. All such occurrences of "NULL" have been replaced now by (void *)NULL, exactly for the reason you mention: NULL is non-portable in this situation.

I never saw this problem on any C-compiler, but I saw it happen in C++ with clang-8.0.1.

Hope this helps, Jan Nijtmans

gustafn3plain2023-10-20 09:02:59
> All such occurrences of "NULL" have been replaced ...

Well, AFIKT not all (at least in the current "trunk" branch after a "fossil pull && fossil update"):
e.g.
   generic/tclBinary.c: 403
   generic/tclBinary.c: 522
   generic/tclCompile.c: 2200
   ...

> ... by (void *)NULL
Maybe, i am too conservative, but "(char *)0L" is fully safe imho.

The problem is, that is that the construct is implementation dependent, 
people use different compilers on different OSes and platforms, .... 
We all know, "not seen so far" is a way is a weak argument.
jan.nijtmanswiki2023-10-20 18:42:39

>Well, AFIKT not all (at least in the current "trunk" branch

That's why this ticket is still "pending"... :-)

jan.nijtmans Closed ticket [26870862f0]. [History diff]2023-12-08 15:40:45
Log in (anonymous is fine) to add a comment.