| Ticket UUID: | 96551aca555c70e4e2157c534c8fdb11acf784e9 | ||
| Title: | Avoid pointer arithmetic with NULL in FOREACH_STRUCT() | ||
| Type: | Patch | Created on: | 2023-04-22 07:23:48 |
| Submitter: | chrstphrchvz | Assigned to: | jan.nijtmans |
| Subsystem: | 35. TclOO Package | Severity: | Minor |
| Priority: | 5 Medium | Last modified: | 2023-05-09 15:00:37 |
| Status: | Closed | Closed by: | jan.nijtmans |
| Resolution: | Fixed | Closed on: | 2023-05-09 15:00:37 |
| Version: | core-8-branch | ||
| Description: | ||||
|
Not aware exactly what Tcl code triggers this, but this -fsanitize=pointer-overflow error is emitted at the beginning of output from
Something like the following is needed (var should also be parenthesized to avoid surprises): --- generic/tclOOInt.h.orig +++ generic/tclOOInt.h @@ -594,7 +594,7 @@ MODULE_SCOPE void TclOOSetupVariableResolver(Tcl_Namespace *nsPtr); */ #define FOREACH_STRUCT(var,ary) \ - for(i=0 ; var=&((ary).list[i]), i<(ary).num; i++) + for(i=0 ; (var)=((ary).list ? &((ary).list[i]) : NULL), i<(ary).num; i++) /* * Convenience macros for iterating through hash tables. FOREACH_HASH_DECLS | ||||
| User Comments: | ||||
jan.nijtmans added on 2023-04-22 18:08:58:
I have doubts on this one. From what I understand from the code "(ary).list" should never be NULL, so if it's NULL somewhere that sounds as a bug. Your fix just hides this bug, so: Yes I would like to know where this is triggered. chrstphrchvz added on 2023-04-22 21:37:53:
This happens somewhere deep within TclOOInit() (so any time an interpreter is created rather than just tcltest), during tclOOScript.tcl:416 ("superclass class" while in "class create singleton …"). chrstphrchvz added on 2023-05-08 00:35:41:
From what I understand from the code "(ary).list" should never be NULL… I believe the comment for FOREACH_STRUCT() is pointing out how, unlike for the FOREACH() macro, (ary).list[i] is never NULL; I do not think it refers to whether (ary).list is NULL. I really am not familiar with TclOO, but what I am finding is that (ary).list == NULL is valid for (ary).num == 0. The specific error at tclOOMethod.c:1108 occurs whenever a class does not have any private variables. jan.nijtmans added on 2023-05-08 09:37:18:
Many thanks for this explanation. It makes sense! How about [0a39ff278486cb6a|this] then? chrstphrchvz added on 2023-05-08 13:12:39:
I do think [0a39ff278486] is better than my initial suggestion (as long as others understand it and are not tempted to undo it), however it looks like there is dependency on the (undocumented) side effect of the macro leaving i == ((ary).num < 0 ? 0 : (ary).num) in InstallPrivateVariableMapping() (tclOODefineCmds.c:552), TclOOReleaseClassContents() (tclOO.c:1089), and ObjectNamespaceDeleted() (tclOO.c:1252), chrstphrchvz added on 2023-05-08 13:37:17:
Maybe something like this will work:
#define FOREACH_STRUCT(var,ary) \
if (i=0, (ary).num>0) for(; var=&((ary).list[i]), i<(ary).num; i++)
jan.nijtmans added on 2023-05-08 13:44:42:
Indeed! You are fully right! Better version [72b13bcbe81628cd|here] jan.nijtmans added on 2023-05-08 13:51:48:
> Maybe something like this will work: I didn't see this suggestion until after I committed my "Better version". I like your version too jan.nijtmans added on 2023-05-08 14:10:25:
Thinking more about it: Your solution is a single C-statement, which has less chance for side-effects than mine. So, I'll follow your suggestion. Thanks! jan.nijtmans added on 2023-05-09 15:00:37:
Now merged to core-8-branch and trunk | ||||
