From: "Peter d" <death_comes_to_all@HOTMAIL.COM>
> ACMD(do_players)
> {
> char buf[MAX_INPUT_LENGTH];
> int i, count = 0;
> *buf = 0;
>
> for (i = 0; i < top_of_p_table; i++) {
> sprintf(buf, "%s %-20.20s", buf, (player_table + i)->name);
> count++;
> if (count == 3) {
> count = 0;
> strcat(buf, "\r\n");
> }
> }
> page_string(ch->desc, buf, 1);
> }
>
> i am using bpl21..
>
This code has som problems, mainly regarding buffer overruns and
overlapping strings in sprintf()[1]. Try this (mailer code):
ACMD(do_players)
{
char buf[MAX_INPUT_LENGTH];
int i, count = 0, len = 0;
*buf = 0;
for (i = 0; i < top_of_p_table; i++)
len += snprintf(buf + len, sizeof(buf) - len,
" %-20.20s%s", player_table[i].name,
(count++ % 3) ? "":"\r\n" );
strncat(buf, "\r\n", sizeof(buf)-len-1);
page_string(ch->desc, buf, 1);
}
Welcor
[1] The practice of using
sprintf(buf, "%s foo", buf);
is dangerous, since, according to the glibc library definitions,
the behaviour of overlapping strings is undefined:
http://www.fsf.org/manual/glibc-2.2.5/html_node/Formatted-Output-Functions.h
tml
"int sprintf (char *s, const char *template, ...)
[...]
The behavior of this function is undefined if copying takes place
between objects that overlap--for example, if s is also given as
an argument to be printed under control of the %s conversion."
Undefined behaviour is bad, since this means different compilers
will behave differently with the results. In this case, they won't
work with your compiler, but may with others.
--
+---------------------------------------------------------------+
| FAQ: http://qsilver.queensu.ca/~fletchra/Circle/list-faq.html |
| Archives: http://post.queensu.ca/listserv/wwwarch/circle.html |
| Newbie List: http://groups.yahoo.com/group/circle-newbies/ |
+---------------------------------------------------------------+
This archive was generated by hypermail 2b30 : 06/25/03 PDT