Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2021-07-26
17:35 • Ticket [f4b0cb2080] Calling Tcl_GetStringFromObj with NULL lengthPtr causes comp... artifact: ba63be506a user: dgp
2021-01-27
06:08 • Ticket [f4b0cb2080]: 5 changes artifact: 0832572d2d user: anticrisis
2021-01-25
10:32 • Closed ticket [f4b0cb2080]. artifact: b0f933c909 user: jan.nijtmans
10:26
Fix [f4b0cb2080] by only accepting new syntax when TCL_NO_DEPRECATED is defined check-in: 0e42f57406 user: jan.nijtmans tags: core-8-branch
08:30 • Ticket [f4b0cb2080] Calling Tcl_GetStringFromObj with NULL lengthPtr causes comp... artifact: af9b447991 user: anticrisis
2021-01-23
03:09 • Ticket [f4b0cb2080]: 4 changes artifact: 1ed8dad761 user: anticrisis
00:09 • Ticket [f4b0cb2080]: 4 changes artifact: 33cb808056 user: anticrisis
2021-01-22
22:38 • New ticket [f4b0cb2080]. artifact: 5b5c7a388f user: anticrisis

Calling Tcl_GetStringFromObj with NULL lengthPtr causes compile error

Closed / Fixedf4b0cb208044f175bdd669db0b293d0bf3f9108eCreated 6 years agoUpdated 5 years ago
Descriptionplain2021-01-22 22:38:12
Code recently introduced in tclDecls.h to handle 64 bit issues causes compile errors when NULL (0) is used as a lengthPtr. This is a common practice when using this function, and is a documented feature.

Here is a godbolt link to an isolated sample. Note that calling Tcl_GetStringFromObj with 0 as the lengthPtr argument causes a compile error.

    https://godbolt.org/z/vxzv53

The problem is this, in tclDecls.h:

#define Tcl_GetStringFromObj(objPtr, sizePtr) \
	(sizeof(*sizePtr) <= sizeof(int) ? tclStubsPtr->tcl_GetStringFromObj(objPtr, (int *)sizePtr) : tclStubsPtr->tclGetStringFromObj(objPtr, (size_t *)sizePtr))

This macro fails if sizePtr is 0, because (*sizePtr) is not valid C code.

Documentation for Tcl_GetStringFromObj indicates that sizePtr may be NULL, but this macro does not allow that.
Referenced by1
  • 0e42f57406Fix [f4b0cb2080] by only accepting new syntax when TCL_NO_DEPRECATED is defined check-in2021-01-25 10:26:22
anticrisisplain2021-01-23 00:09:32
I'm not sure I understand the logic of these macros. Is the Tcl C API changing from using int to using size_t? Wouldn't it be easier to simply stay with int, which will always be the same size on a given platform and compiler?

It seems these macros are intending to support the case where a particular C extension may sometimes use int and sometimes use size_t. But shouldn't the C extension conform to the interface provided by the API documentation? If we're changing to size_t, then people will have to update their code. If we're staying with int, then we don't need these macros.
anticrisisplain2021-01-23 03:09:30
I found TIP 481, which indicates we are intending to support both int and size_t arguments. I just don’t know how a macro can work when 0 is a valid argument.
anticrisis Ticket [f4b0cb2080]: 5 changes [History diff]2021-01-25 08:30:06
jan.nijtmanswiki2021-01-25 10:32:18

It is true that '0' as argument doesn't work, but NULL does! Since lengthPtr is a pointer to an integer, it - actually - should be (int *)NULL, and that works too. However, since '0' was accepted as argument in Tcl 8.6, we cannot just change that in a minor release, pity. In Tcl 9.0, strings can be larger than MAX_INT, this group of functions should - then - be able to account for that.

In Tcl 9.0, either use an (int *)NULL argument, or use the function Tcl_GetString() (which is a macro doing the same). It's a small burden for extensions to make that change, but it opens up the 64-bit world for string lengths, that's what TIP 481 is about.

Thanks for your report!

anticrisisplain2021-01-27 06:08:48
Ah, thank you! I learned something about C today. And, sqlite now compiles successfully again with core-8-branch.
dgpplain2021-07-26 17:35:29
I cannot reproduce the issue reported here, but I created a branch
bug-f4b0cb2080 with a potential fix.  Any comment or testing?
Log in (anonymous is fine) to add a comment.