Jump to content
The Dark Mod Forums

Dubious use of postfix increment operator (possible bug)


Hamlet

Recommended Posts

While analysing the warnings emitted by GCC 6, I was pointed to this piece of code in renderer/Model_lwo.cpp (comment added):

int sgetI1( unsigned char **bp )
{
   int i;

   if ( flen == FLEN_ERROR ) return 0;
   i = **bp;
   if ( i > 127 ) i -= 256;
   flen += 1;
   *bp++;       // <== warning: unused value
   return i;
}


short sgetI2( unsigned char **bp )
{
   short i;

   if ( flen == FLEN_ERROR ) return 0;
   memcpy( &i, *bp, 2 );
   BigRevBytes( &i, 2, 1 );
   flen += 2;
   *bp += 2;
   return i;
} 
Starting from the second function, sgetI2(), it appears that it receives as argument a pointer to an unsigned character, passed by reference in C style (that becomes a pointer to the pointer).

It copies two bytes in a short, swap them as needed (handling little/big endian, I suppose), then it makes the pointer *bp point after the copied data and returns the value read.

There is a sgetI4() which does the same with 4 byte data.

Back to the first quoted, I was assuming sgetI1() would do the same. GCC complains that the statement *bpp++ produces an unused value. It turns out, the postfix increment operator (a++) has the highest priority among C and C++ operators, and it gets executed before the dereference operator. That means that I would expect the code to operate like (*bp)++, equivalent to *bp += 1, similarly to sgetI2(), but I get instead a *(bp++). This means that the local variable bp (unsigned char**) is increased (while the unsigned char* pointer *bp is not), and then the value it was pointing to before the increase is taken and ignored.

All in all, it means that when it's called as sgetI1(&ptrToBuffer), it returns the value at the current value of ptrToBuffer and, on the next call, sgetI1(&ptrToBuffer), it returns again the same value, since ptrToBuffer is not increased.

 

The same construct appears in sgetU1() and in add_clip() on nclips (a simple pointer int*), where it might have more serious consequences:

   *nclips++;
   clip->index = *nclips;
since the an index is assigned from the cell of memory next to nclips (nclips is first increased).

I can't find any place where the former two are used, while add_clip() is pulled in from LoadLWO() in renderer/Model.cpp (at which point I stopped tracking).

 

I attach a trivial patch that removes these warnings by just doing the thing that would be usually expected (increase the pointed value) (it also removes the other instance of it, not mentioned in this text).

 

 

Edit: discovered that syntax highlight for C/C++ works with code type "auto", even if it does not show in the preview.

UnusedValue.patch.txt

Edited by Hamlet
  • Like 2
Link to comment
Share on other sites

Is this code ever executed and does the fixed code cause any instantly visible problems?

Not sure if TDM uses LWO's anywhere?

My understanding is too small to answer the first question: I learned what LWO means after reading the previous post.

I also don't know what a "clip" is.

The main reason of my post is to see if the experts have anything to add to the picture, that might predict a type of failure or to connect this to an already observed one.

I can see the dubious function being called when a ID_RIMG or ID_TIMG is found, in code that looks like a parser. The former seems related to reflection effects on a surface, the latter ends in the parameters of a texture. Maybe there might be effects related to clipping of these surfaces and textures? Just wildly guessing here.

 

About the patch: I do not claim to have tested it properly yet, and as always I personally recommend not merging it into anything official until that happens.

Link to comment
Share on other sites

Clip is a special texture that imbues a surface with physics attributes.

Plain clip is used for the collision model, playerclip keeps a the player from

entering an area, and monsterclip is used to ensure that AI can properly pathfind

by simplifying the path generation.

Please visit TDM's IndieDB site and help promote the mod:

 

http://www.indiedb.com/mods/the-dark-mod

 

(Yeah, shameless promotion... but traffic is traffic folks...)

Link to comment
Share on other sites

Hamlet,

I'm a little late with this advice, but I've just loaded a TDM map in DarkRadiant (DR) for the 1st time. And since you've already installed DR, if you extract one of mission PK4 files (I used the simplistic "Closemouthed Shadows" mission) and load the map in DR, you'll get a nice window showing various "Clip" and "Monster Clip" (etc) objects, rendered in 3D and nicely labeled.

As for your patches to address these compile-time warnings, I have not forgotten about them. I plan to look into any/all of them more sometime after the 2.05 build problems are put to rest. I think some of these changes may meet some resistance, but I still think they're worth further investigation, so I hope you haven't begun to think that I'm ignoring your patches. There's just so much to do and so little time (for me).

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recent Status Updates

    • OrbWeaver

      Does anyone actually use the Normalise button in the Surface inspector? Even after looking at the code I'm not quite sure what it's for.
      · 6 replies
    • Ansome

      Turns out my 15th anniversary mission idea has already been done once or twice before! I've been beaten to the punch once again, but I suppose that's to be expected when there's over 170 FMs out there, eh? I'm not complaining though, I love learning new tricks and taking inspiration from past FMs. Best of luck on your own fan missions!
      · 4 replies
    • The Black Arrow

      I wanna play Doom 3, but fhDoom has much better features than dhewm3, yet fhDoom is old, outdated and probably not supported. Damn!
      Makes me think that TDM engine for Doom 3 itself would actually be perfect.
      · 6 replies
    • Petike the Taffer

      Maybe a bit of advice ? In the FM series I'm preparing, the two main characters have the given names Toby and Agnes (it's the protagonist and deuteragonist, respectively), I've been toying with the idea of giving them family names as well, since many of the FM series have named protagonists who have surnames. Toby's from a family who were usually farriers, though he eventually wound up working as a cobbler (this serves as a daylight "front" for his night time thieving). Would it make sense if the man's popularly accepted family name was Farrier ? It's an existing, though less common English surname, and it directly refers to the profession practiced by his relatives. Your suggestions ?
      · 9 replies
    • nbohr1more

      Looks like the "Reverse April Fools" releases were too well hidden. Darkfate still hasn't acknowledge all the new releases. Did you play any of the new April Fools missions?
      · 5 replies
×
×
  • Create New...