Skip to content

Fix setAmxString cutting off the last character of strings#385

Merged
s1lentq merged 1 commit into
rehlds:masterfrom
metita:harden-setamxstring-bounds
Jul 20, 2026
Merged

Fix setAmxString cutting off the last character of strings#385
s1lentq merged 1 commit into
rehlds:masterfrom
metita:harden-setamxstring-bounds

Conversation

@metita

@metita metita commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Fixes #383.

3c08fde changed setAmxString to treat max as the full buffer size and reserve a cell for the null terminator. But plugins pass charsmax(), which already excludes the null, so every native that returns a string (get_entvar, get_member, ...) ended up dropping the last character.

This reverts the length handling back to the original charsmax behavior.

@metita

metita commented Jul 20, 2026

Copy link
Copy Markdown
Contributor Author

tested on a live 5.29 server, same version so the only diff is the patch.

vanilla:

[A] tight='info_targe' -> FAIL (truncated)
[B] sizeof='info_target' -> correct

patched:

[A] tight='info_target' -> PASS
[B] sizeof='info_target' -> correct

[A] is the check: tight buffer + charsmax, vanilla drops the last char, patched keeps it. sizeof works in both.

test plugin:

#include <amxmodx>
#include <reapi>

public plugin_init()
{
    register_plugin("setAmxString test", "1.0", "test")
    register_srvcmd("test_setamxstr", "cmd_test")
}

public cmd_test()
{
    new ent = rg_create_entity("info_target")
    if (!is_entity(ent)) return PLUGIN_HANDLED;

    new tight[12] // "info_target" = 11 chars, charsmax = 11
    get_entvar(ent, var_classname, tight, charsmax(tight))
    server_print("[A] tight='%s' -> %s", tight, equal(tight, "info_target") ? "PASS" : "FAIL")

    new tight2[12]
    get_entvar(ent, var_classname, tight2, sizeof(tight2))
    server_print("[B] sizeof='%s' -> %s", tight2, equal(tight2, "info_target") ? "correct" : "differs")

    rg_remove_entity(ent)
    return PLUGIN_HANDLED;
}

@metita
metita force-pushed the harden-setamxstring-bounds branch from 3bfdb6f to 8be9578 Compare July 20, 2026 05:57
@metita metita changed the title Clamp setAmxString to the plugin buffer to prevent overflow crash Fix setAmxString cutting off the last character of strings Jul 20, 2026
@s1lentq
s1lentq merged commit 343f157 into rehlds:master Jul 20, 2026
3 checks passed
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.

[BUG]: Wrong var lenghts

2 participants