Artifacts Associated With Ticket 6ef312dcf5
Ticket change [b21da8d5a8] (rid 196822) by chrstphrchvz on 2021-12-16 04:31:11:
- assignee initialized to: "nobody"
- closer initialized to: "nobody"
- cmimetype initialized to: "text/x-fossil-wiki"
- comment initialized to:
If <tt>Tcl_CreateMathFunc()</tt> (generic/tclBasic.c) is called with 0 and <tt>NULL</tt> for <tt>numArgs</tt> and <tt>argTypes</tt> respectively, as done in <tt>Tcltest_Init()</tt> (generic/tclTest.c), this causes <tt>memcpy()</tt> to be called with <tt>NULL</tt> as the source, which is undefined behavior even though the number of bytes to copy is 0. [https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html#available-checks|UBSan (<tt>-fsanitize=nonnull-attribute</tt>)] complaint when used with GCC (as not all common standard library implementations declare <tt>memcpy()</tt> with source or destination pointers as non-<tt>NULL</tt>): <pre> $ ./tcltest <span style="font-weight: bold">tcl/generic/tclBasic.c:3557:2: <span style="color:red">runtime error:</span> null pointer passed as argument 2, which is declared to never be null</span> % </pre> To avoid this, I would only call <tt>memcpy()</tt> (and <tt>ckalloc()</tt>) when <tt>numArgs</tt> is positive: <pre> diff --git generic/tclBasic.c generic/tclBasic.c index fb8524180..45a25ca64 100644 --- generic/tclBasic.c +++ generic/tclBasic.c @@ -3550,8 +3550,12 @@ Tcl_CreateMathFunc( <span> </span> data->proc = proc; data->numArgs = numArgs; <span style="color:red">- data->argTypes = (Tcl_ValueType *)ckalloc(numArgs * sizeof(Tcl_ValueType)); - memcpy(data->argTypes, argTypes, numArgs * sizeof(Tcl_ValueType));</span> <span style="color:green">+ data->argTypes = NULL; + if (0 < numArgs) { + data->argTypes = + (Tcl_ValueType *)ckalloc(numArgs * sizeof(Tcl_ValueType)); + memcpy(data->argTypes, argTypes, numArgs * sizeof(Tcl_ValueType)); + }</span> data->clientData = clientData; <span> </span> Tcl_DStringInit(&bigName); </pre> - foundin initialized to: "8.6.12"
- is_private initialized to: "0"
- login: "chrstphrchvz"
- priority initialized to: "5 Medium"
- resolution initialized to: "None"
- severity initialized to: "Minor"
- status initialized to: "Open"
- submitter initialized to: "chrstphrchvz"
- subsystem initialized to: "52. Portability Support"
- title initialized to: "memcpy() with NULL source in Tcl_CreateMathFunc()"
- type initialized to: "Patch"
Ticket change [58f6c24ae6] (rid 196846) by jan.nijtmans on 2021-12-16 14:16:24:
- icomment:
Fixed [c8b818c8cd|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!
- login: "jan.nijtmans"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [61992654e9] (rid 196847) by jan.nijtmans on 2021-12-16 14:17:10:
- assignee changed to: "jan.nijtmans"
- closedate changed to: "2459565.09525907"
- closer changed to: "jan.nijtmans"
- login: "jan.nijtmans"
- mimetype: "text/plain"
- resolution changed to: "Fixed"
- status changed to: "Closed"