|
2021-12-16
| ||
| 14:17 | • Closed ticket [6ef312dcf5]: memcpy() with NULL source in Tcl_CreateMathFunc()</i... artifact: 61992654e9 user: jan.nijtmans | |
| 14:16 | • Ticket [6ef312dcf5]: 3 changes artifact: 58f6c24ae6 user: jan.nijtmans | |
| 09:02 | Fix [6ef312dcf5]: memcpy() with NULL source in Tcl_CreateMathFunc() check-in: c8b818c8cd user: jan.nijtmans tags: core-8-6-branch | |
| 04:31 | • New ticket [6ef312dcf5] memcpy() with NULL source in Tcl_CreateMathFunc(). artifact: b21da8d5a8 user: chrstphrchvz | |
memcpy() with NULL source in Tcl_CreateMathFunc()
Closed / Fixed6ef312dcf5b846c344911c04a1e41e52ec28efe2Created 5 years agoUpdated 5 years ago
Descriptionwiki2021-12-16 04:31:11
If Tcl_CreateMathFunc() (generic/tclBasic.c) is called with 0 and NULL for numArgs and argTypes respectively, as done in Tcltest_Init() (generic/tclTest.c), this causes memcpy() to be called with NULL as the source, which is undefined behavior even though the number of bytes to copy is 0. UBSan (-fsanitize=nonnull-attribute) complaint when used with GCC (as not all common standard library implementations declare memcpy() with source or destination pointers as non-NULL):
$ ./tcltest
tcl/generic/tclBasic.c:3557:2: runtime error: null pointer passed as argument 2, which is declared to never be null
%
To avoid this, I would only call memcpy() (and ckalloc()) when numArgs is positive:
diff --git generic/tclBasic.c generic/tclBasic.c index fb8524180..45a25ca64 100644 --- generic/tclBasic.c +++ generic/tclBasic.c @@ -3550,8 +3550,12 @@ Tcl_CreateMathFunc( data->proc = proc; data->numArgs = numArgs; - data->argTypes = (Tcl_ValueType *)ckalloc(numArgs * sizeof(Tcl_ValueType)); - memcpy(data->argTypes, argTypes, numArgs * sizeof(Tcl_ValueType)); + data->argTypes = NULL; + if (0 < numArgs) { + data->argTypes = + (Tcl_ValueType *)ckalloc(numArgs * sizeof(Tcl_ValueType)); + memcpy(data->argTypes, argTypes, numArgs * sizeof(Tcl_ValueType)); + } data->clientData = clientData; Tcl_DStringInit(&bigName);
Referenced by1
c8b818c8cdFix [6ef312dcf5]: memcpy() with NULL source in Tcl_CreateMathFunc() check-in2021-12-16 09:02:59
jan.nijtmanswiki2021-12-16 14:16:24
Fixed [here]. Didn't bother about the ckalloc(), since Tcl_CreateMathFunc() is deprecated (it's totally removed in Tcl 9.0). Setting data->argTypes to NULL where it was not NULL before, I'm not sure what other possible ill-effects that could mean for extensions using Tcl_CreateMathFunc(). This isn't called by Tcl core any more, so testcases don't tell the whole story here.
Thanks for the report and the patch!
jan.nijtmans Closed ticket [6ef312dcf5].
[History diff]2021-12-16 14:17:10
Log in (anonymous is fine) to add a comment.