ko4life-net / ko

Open source development of the game Knight Online. This is a reversed engineered old version of the game aiming to replicate the nostalgic experience we all once had <3
https://github.com/ko4life-net/ko
MIT License
55 stars 22 forks source link

Ensure correct handling of null-termination in N3ME string writing. #257

Closed stevewgr closed 1 week ago

stevewgr commented 1 week ago

Description

This pull request addresses an official bug in N3ME by ensuring that the null-termination byte is correctly included when writing strings to files. This fix resolves issues caused by incorrect string length calculations, which previously led to inconsistencies during file operations. This issue, present in official maps from the 2xxx versions, could lead to confusion or errors for users interpreting these formats.

Root Cause

The root cause of this bug lies in the process of reading strings from files. Specifically:

Compatibility Note

To remain compatible with official formats, we must include the null-termination byte, even though it adds redundancy.

Why the Bug Was Overlooked

This issue went unnoticed because the client reads the string into a char buffer[len], then constructs an std::string from it. Although the binary format includes the redundant null byte, std::string stops at the first null-termination, so the extra byte remained hidden. This effectively made the binary format’s string length 1 byte longer than necessary.

stevewgr commented 1 week ago

Build passing for the record: https://github.com/ko4life-net/ko/actions/runs/11809256411

but I'll still wait for review before merging.