Skip to content

Commit

Permalink
Fix: oclass was not read properly from swapchest file
Browse files Browse the repository at this point in the history
... after the change earlier to silence a compiler warning. Reading a
character from the file is incorrect since we write oclass as an
integer, so it would probably attempt to read the first digit of that
integer as a character and set the resulting object's class to an
invalid value. Who knows what undefined behavior would have resulted.

Instead, read the oclass from the file into an int variable, then cast
that to char for setting the actual object's class.
  • Loading branch information
copperwater committed Oct 15, 2024
1 parent a7eaecb commit 2787b61
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/files.c
Original file line number Diff line number Diff line change
Expand Up @@ -4924,8 +4924,12 @@ short *rcode;
continue;
if (sscanf(buf, "spe=%hhd", &(o->spe)) == 1)
continue;
if (sscanf(buf, "oclass=%c", &(o->oclass)) == 1)
if (sscanf(buf, "oclass=%d", &tmp_bitfield) == 1) {
/* oclass isn't a bitfield but read it as an int to silence a
* compiler warning for reading an int directly into a char */
o->oclass = (char) tmp_bitfield;
continue;
}
if (sscanf(buf, "cursed=%d", &tmp_bitfield) == 1) {
o->cursed = tmp_bitfield;
continue;
Expand Down

0 comments on commit 2787b61

Please sign in to comment.