Skip to content

File.ReadString doesn't predictably advance file when destination buffer is too short #1430

Open
@dysphie

Description

@dysphie

Help us help you

  • I have checked that my issue doesn't exist yet.
  • I have tried my absolute best to reduce the problem-space and have provided the absolute smallest test-case possible.
  • I can always reproduce the issue with the provided description below.

Environment

  • Current SourceMod version: 1.11.0.6654

Description

Current behavior of File.ReadString makes it cumbersome to read indefinite length strings and data after it.

Problematic Code (or Steps to Reproduce)

Suppose we've just created a file. It contains an indeterminate length string, and a value after it:

File file = OpenFile("test_file", "wb");
file.WriteString("please_insert_batteries", true);
file.WriteInt32(123);
delete file;

Complementary code that reads from it:

File file = OpenFile("test_file", "rb");
char buffer[10]; // too small
file.ReadString(buffer, sizeof(buffer), -1);
int value;
file.ReadInt32(value);
PrintToServer("%d", value);
delete file;

Expected behavior:

file.ReadString reads the string, advancing the position in the file until a null terminator is found, copying the bytes into the buffer and discarding any excess. Then it reads the int32 value.

Actual behavior:

file.ReadString reads the string, advancing the position in the file until a null terminator is found or the buffer is consumed.
If the latter happens the position in the file is now unclear. We can't safely read the int32 value.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Buggeneral bugs; can be anythingHelp Wantedup for grabs

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions