Avoid pointer arithmetic with NULL in FOREACH_STRUCT()
generic/tclOOMethod.c:1108:2: runtime error: applying zero offset to null pointer
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
5be2687f62Fix [96551aca55]: Avoid pointer arithmetic with NULL in FOREACH_STRUCT(). Some typo's check-in2023-05-08 14:19:040a39ff2784Proposed fix for [96551aca55]: Avoid pointer arithmetic with NULL in FOREACH_STRUCT() check-in2023-05-08 09:35:57
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.
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 …").
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.
Many thanks for this explanation. It makes sense!
How about [this] then?
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),
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++)
Indeed! You are fully right! Better version [here]
> Maybe something like this will work:
I didn't see this suggestion until after I committed my "Better version". I like your version too
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!
Now merged to core-8-branch and trunk