sprintf not using the right strings some times?
Samual created issue #929 (closed) on 2011-12-24T16:46:14Z:
I have been working on a feature for my updatecommands branch which is basically a player-listing command, called “who.” — The problem I am running into is, some times with nested IF functions, the sprintf outputs the wrong string even though the code is right.
I tried strzone’ing the strings in question, thinking maybe this was fteqcc’s fault, but then when I went to unzone it, I got a host_error saying the string is owned by the engine… this makes me think it’s sprintf’s fault.
Anyway, the code is here… and what happens is, the last two parameters (net address and crypto key) when privacy is on don’t say “hidden,” but they say some other random string from the function.
void CommonCommand_who(float request, entity caller, float argc)
{
switch(request)
{
case CMD_REQUEST_COMMAND:
{
float total_listed_players, tmp_hours, tmp_minutes, tmp_seconds, is_bot;
entity tmp_player;
float privacy = (caller && autocvar_sv_status_privacy);
string separator = strcat((argv(1) ? argv(1) : " "), "^7");
print_to(caller, strcat("List of client information", (privacy ? " (some data is hidden for privacy)" : string_null), ":"));
print_to(caller, sprintf(strreplace(" ", separator, " %-4s %-20s %-5s %-3s %-9s %-16s %s "),
"ent", "nickname", "ping", "pl", "time", "ip", "crypto_id"));
FOR_EACH_CLIENT(tmp_player)
{
is_bot = (clienttype(tmp_player) == CLIENTTYPE_BOT);
tmp_hours = tmp_minutes = tmp_seconds = 0;
tmp_seconds = floor(time - tmp_player.jointime);
tmp_minutes = floor(tmp_seconds / 60);
tmp_hours = floor(tmp_minutes / 60);
if(tmp_minutes) { tmp_seconds -= (tmp_minutes * 60); }
if(tmp_hours) { tmp_minutes -= (tmp_hours * 60); }
print_to(caller, sprintf(strreplace(" ", separator, " %-4s %-20.20s %-5d %-3d %-9s %-16s %s "),
strcat("#", ftos(num_for_edict(tmp_player))),
tmp_player.netname,
tmp_player.ping,
tmp_player.ping_packetloss,
sprintf("%02d:%02d:%02d", tmp_hours, tmp_minutes, tmp_seconds),
(is_bot ? "null/botclient" : (privacy ? "hidden" : tmp_player.netaddress)),
(is_bot ? "null/botclient" : (privacy ? "hidden" : tmp_player.crypto_idfp))));
++total_listed_players;
}
print_to(caller, strcat("Finished listing ", ftos(total_listed_players), " client(s) out of ", ftos(maxclients), " slots."));
return; // never fall through to usage
}
default:
case CMD_REQUEST_USAGE:
{
print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " who [separator]"));
print_to(caller, " Where 'separator' is the optional string to separate the values with, default is a space.");
return;
}
}
}