Skip to content

dsub.c: Fix format string type mismatch in database error messages #39

@justwheel

Description

@justwheel

Summary

Three fprintf calls in dsub.c use %d to format long int values, producing undefined behavior on 64-bit platforms.

Background

The CI pipeline introduced in PR #38 revealed three -Wformat warnings from both GCC and Clang when compiling dsub.c.
The function rspsb2nl_ declares x as long (line 67) and iloc is assigned from ftell() which returns long (line 111), but the error-reporting fprintf calls on lines 84, 96, and 114 all use %d (which expects int).
On 64-bit platforms where long is 8 bytes and int is 4 bytes, this is undefined behavior per the C standard and could print garbled values in database error messages.

Details

  1. dsub.c:84fprintf(stderr, "Error seeking database loc %d\n", x);x is long
  2. dsub.c:96fprintf(stderr, "Error reading database loc %d\n", x);x is long
  3. dsub.c:114fprintf(stderr, "Error seeking database loc %d\n", iloc);iloc is long (from ftell())

Fix: change %d to %ld in all three calls.
This does not alter game logic — it only corrects the format specifiers in error-handling code paths.

This can be fixed as part of v1.1.0, but will most likely be addressed in a future release to help get recent changes submitted downstream in the Fedora package.

Outcome

The codebase compiles without -Wformat warnings and database error messages display correct seek/read positions on all platforms.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething is broken or produces incorrect behaviorcompiler warningDiscovered via compiler diagnostics

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions