Skip to content
Tcl Source Code View Ticket
Login

View Ticket

Avoid pointer arithmetic with NULL in FOREACH_STRUCT()

Closed / Fixed96551aca555c70e4e2157c534c8fdb11acf784e9Created 4 years agoUpdated 4 years ago
Descriptionwiki2023-04-22 07:23:48
Not aware exactly what Tcl code triggers this, but this -fsanitize=pointer-overflow error is emitted at the beginning of output from tcltest during make test tclsh:
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

Referenced by2
  • 5be2687f62Fix [96551aca55]: Avoid pointer arithmetic with NULL in FOREACH_STRUCT(). Some typo's check-in2023-05-08 14:19:04
  • 0a39ff2784Proposed fix for [96551aca55]: Avoid pointer arithmetic with NULL in FOREACH_STRUCT() check-in2023-05-08 09:35:57
chrstphrchvz Ticket [96551aca55]: 3 changes [History diff]2023-04-22 07:32:11
jan.nijtmanswiki2023-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.

chrstphrchvzwiki2023-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 …").

chrstphrchvzwiki2023-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.nijtmanswiki2023-05-08 09:37:18

Many thanks for this explanation. It makes sense!

How about [this] then?

chrstphrchvzwiki2023-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),

chrstphrchvzwiki2023-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.nijtmanswiki2023-05-08 13:44:42

Indeed! You are fully right! Better version [here]

jan.nijtmanswiki2023-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.nijtmanswiki2023-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.nijtmanswiki2023-05-09 15:00:37

Now merged to core-8-branch and trunk

Log in (anonymous is fine) to add a comment.