Null pointer refererce

19 views
Skip to first unread message

Przemysław Kamiński (XLII)

unread,
Jul 15, 2025, 10:57:35 PMJul 15
to CLIPSESG
Hi,

I'm doing so Zig<->Go<->CLIPS integration and had an issue trying to compile  CLIPSusing Zig,. It complained about null reference during Environment initialization. Quick and dirty patch (I don't have C tooling installed, so no lints, sorry!)

That was using CLIPS_642 source code.

Best,
Przemysław Alexander Kamiński

*** classcom.c 2023-02-01 01:41:53.000000000
--- classcom.c 2025-07-14 17:47:26.000000000
***************
*** 350,360 ****
--- 350,364 ----
                      is returned.
   ***********************************************************/
  Defclass *GetNextDefclass(
    Environment *theEnv,
    Defclass *theDefclass)
    {
+     if(theDefclass == NULL) {
+       return NULL;
+     }
 
     return (Defclass *) GetNextConstructItem(theEnv,&theDefclass->header,
                                              DefclassData(theEnv)->DefclassModuleIndex);
    }
 
  /***************************************************
*** tmpltdef.c 2023-02-01 01:41:53.000000000
--- tmpltdef.c 2025-07-14 17:52:58.000000000
***************
*** 285,296 ****
--- 285,299 ----
  /*   deftemplate following the deftemplate passed as an argument.      */
  /***********************************************************************/
  Deftemplate *GetNextDeftemplate(
    Environment *theEnv,
    Deftemplate *deftemplatePtr)
    {
+     if(deftemplatePtr == NULL) {
+       return NULL;
+     }
     return (Deftemplate *) GetNextConstructItem(theEnv,&deftemplatePtr->header,DeftemplateData(theEnv)->DeftemplateModuleIndex);
    }
 
  /**********************************************************/
  /* DeftemplateIsDeletable: Returns true if a particular   */
  /*   deftemplate can be deleted, otherwise returns false. */

CLIPS Support

unread,
Jul 16, 2025, 12:46:19 AMJul 16
to CLIPSESG
These patches will break every piece of code that calls them. The functions you've patched return the first item in a list if passed NULL, but you've redefined them to always return NULL.

/***********************************************************************/

/* GetNextDeftemplate: If passed a NULL pointer, returns the first     */

/*   deftemplate in the ListOfDeftemplates. Otherwise returns the next */

/*   deftemplate following the deftemplate passed as an argument.      */

/***********************************************************************/

Deftemplate *GetNextDeftemplate(

  Environment *theEnv,

  Deftemplate *deftemplatePtr)

  {

   return (Deftemplate *) GetNextConstructItem(theEnv,&deftemplatePtr->header,DeftemplateData(theEnv)->DeftemplateModuleIndex);

  }



Przemysław Kamiński (XLII)

unread,
Jul 16, 2025, 1:20:22 PMJul 16
to CLIPSESG
Make sense.
Still if `deftemplatePtr` is NULL then dereferencing is something that compiler can catch.

So this should be something like:

deftemplatePtr == NULL ? NULL : &deftemplatePtr->Header


right?

CLIPS Support

unread,
Jul 16, 2025, 3:43:02 PMJul 16
to CLIPSESG
I just checked in some fixes to the SourceForge repository before I saw your most recent message. I did the more verbose version of what you've suggested, but made the change to the GetNext functions for all the constructs. Since header is intentionally  the first member of the struct, I think &deftemplatePtr->header always resolve to deftemplatePtr, even if deftemplatePtr is NULL, but I can see how a compiler might have an issue with this if it's doing an analysis of the code.

Deftemplate *GetNextDeftemplate(

  Environment *theEnv,

  Deftemplate *deftemplatePtr)

  {

   ConstructHeader *theHeader;

   

   if (deftemplatePtr == NULL)

     { theHeader = NULL; }

   else

     { theHeader = &deftemplatePtr->header; }

     

   return (Deftemplate *) GetNextConstructItem(theEnv,theHeader,DeftemplateData(theEnv)->DeftemplateModuleIndex);

  }



Przemysław Kamiński (XLII)

unread,
Jul 17, 2025, 1:10:08 PMJul 17
to CLIPSESG
Oh, that's fine. I just thought to share, because being able to cross-compile easily using Zig I can integrate it easier, thought someone else might encounter the same issue.
Thank you for pointing that out though, it explained why I started having surprising errors stemming from that. I think I found 3 places in total where this pattern is used, but after that compilation went through.

Best,
Przemysław Alexander Kamiński

Reply all
Reply to author
Forward
0 new messages