Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2024-08-24
18:42
Merge [ddc792ce8c0c9612]: Use "(char *)NULL" as sentinel in documentation. See: [26870862f0]. check-in: ea45b32916 user: pooryorick tags: unchained, INCOMPATIBLE_LICENSE
2024-08-22
21:15
Fossil merge [e6127d0a7b2b0f53]: [26870862f0] follow-up: of course it was not complete yet. check-in: d596742ed0 user: pooryorick tags: unchained, INCOMPATIBLE_LICENSE
21:12
Merge [506bbed9ce503ee0]: Remaining fixes for [26870862f0]: Wrong sentinel in Tcl_SetErrorCode usage... check-in: e700392910 user: pooryorick tags: unchained, INCOMPATIBLE_LICENSE
20:52
Merge [f67f60d202716adf]: Fix [26870862f0]: Wrong sentinel in Tcl_SetErrorCode usage; Handle any C++... check-in: e18c9ab9f6 user: pooryorick tags: unchained, INCOMPATIBLE_LICENSE
2023-12-08
15:40 • Closed ticket [26870862f0]: Wrong sentinel in Tcl_SetErrorCode usage plus 4 ... artifact: a1d144e9e4 user: jan.nijtmans
15:20
Use "(char *)NULL" as sentinel in documentation. See: [26870862f0] check-in: 4e8d7db3a5 user: jan.nijtmans tags: core-8-6-branch
2023-10-27
17:13
[26870862f0] follow-up: of course it was not complete yet check-in: d2309f150a user: jan.nijtmans tags: core-8-branch
2023-10-22
19:49
Remaining fixes for [26870862f0]: Wrong sentinel in Tcl_SetErrorCode usage check-in: 506bbed9ce user: jan.nijtmans tags: trunk, main
2023-10-20
18:42 • Ticket [26870862f0] Wrong sentinel in Tcl_SetErrorCode usage status still Pe... artifact: 3329b6a1b8 user: jan.nijtmans
09:35
(cherrypick) small part of: [26870862f0]: Wrong sentinel in Tcl_SetErrorCode usage; Handle any C++-c... check-in: 584bb57e0b user: jan.nijtmans tags: core-8-6-branch
09:02 • Ticket [26870862f0] Wrong sentinel in Tcl_SetErrorCode usage status still Pe... artifact: 2cc9da4a5b user: gustafn3
08:14 • Ticket [26870862f0]: 3 changes artifact: f91019b8f5 user: jan.nijtmans
08:14 • Ticket [26870862f0]: 3 changes artifact: 6c419b899c user: jan.nijtmans
07:53 • Ticket [26870862f0]: 3 changes artifact: f9a87c64ab user: gustafn3
2023-10-19
18:19 • Pending ticket [26870862f0]. artifact: fdee532212 user: jan.nijtmans
18:09
Fix [26870862f0]: Wrong sentinel in Tcl_SetErrorCode usage; Handle any C++-compiler as well check-in: 234ff497c4 user: jan.nijtmans tags: core-8-branch
2023-10-15
22:03 • Ticket [26870862f0] Wrong sentinel in Tcl_SetErrorCode usage status still Op... artifact: 16d5a9cd0b user: jan.nijtmans
11:51
Fix [26870862f0]: Wrong sentinel in Tcl_SetErrorCode usage check-in: 1317d7d04f user: jan.nijtmans tags: core-8-6-branch
11:51 • New ticket [26870862f0] Wrong sentinel in Tcl_SetErrorCode usage. artifact: f6e3968cbc user: jan.nijtmans

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.