Skip to content

Count the whole C string in string_frm - #1068

Open
Reveares wants to merge 3 commits into
Try:masterfrom
Reveares:fix/string-frm-count
Open

Reveares wants to merge 3 commits into
Try:masterfrom
Reveares:fix/string-frm-count

Conversation

@Reveares

Copy link
Copy Markdown
Contributor

string_frm formats twice: into its 64-byte buffer to measure the text, then into a heap buffer of that size. The const char* overload stopped counting once the 64-byte buffer was full, so a C string argument after that point was measured short and the second pass wrote past the heap buffer.

This happens for the sky texture. It formats SKYDAY_<stem>_LAYER<n>_A0.TGA with the world's name, so any world whose file name stem is 41 bytes or longer overruns the heap. ASan reports a heap-buffer-overflow in implWrite.

The const char* overload now forwards to the string_view one.

@Reveares
Reveares force-pushed the fix/string-frm-count branch from a8b2f56 to 2df108e Compare November 19, 2026 23:10
@Try

Try commented Nov 19, 2026

Copy link
Copy Markdown
Owner

Hi, @Reveares and thanks for the find!

Forwarding to std::string_view is somewhat suboptimal (will cause extra call to strlen). I've return const char* version, but have no use-case to test - may you check it please?

@Reveares

Reveares commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor Author

Yes, keeping the specialized version also works. Your version was correct, but CI failed.
I corrected it and removed the second for because it's not needed.

To verify you can build with ASan, rename a ZEN to a long name and pass it with -w to OpenGothic.
Or run the following test.

#include <cstdint>
#include <cstdio>
#include <string>
#include "string_frm.h"

int main(int argc, char**) {
  string_frm warn("RaysCasted: ", argc); // GCC: -Warray-bounds on PR 45e69d6
  for(size_t n=0; n<200; ++n) {
    std::string a(n, 'a');
    string_frm s(a.c_str(), 'b', a.c_str());
    if(std::string_view(s) != a+'b'+a)
      std::printf("FAIL at %zu\n", n);
      return 1;
      }
  std::puts(warn.c_str());
  return 0;
  }

g++ -std=c++20 -O2 -Wall -Werror -I common/utils t.cpp && ./a.out

On master this will output
FAIL at 40

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants