On 2015-11-13 at 5:34 'Davide Libenzi' via Akaros wrote:
> I argue that strcat() as interface is bad enough done once, but done
> in a loop, by continuously throwing away the buffer length at every
> call, is even evil.
It might not be high performance, but from looking at the code it is
much more clear what it is doing and bugs are harder to hide:
> + for (i = 0; cmds[i]; i++) {
>> > + csize = strlen(cmds[i]);
>> > + if ((csize + size + 2) > sizeof(msgbuf))
>> > + break;
>> > + msgbuf[size] = '|';
>> > + memcpy(msgbuf + size + 1, cmds[i], csize);
>> > + size += csize + 1;
>> > + }
>> > + msgbuf[size] = 0;
this has a lot of "+2" and +1. hope we don't have an off-by-one
error! or forget the trailing msgbuf[size] = 0;
> This loop could be replaced with,
>
> strlcpy(msgbuf, ctlstring, sizeof(msgbuf));
> for (int i = 0; i < nelem(cmds); i++) {
> strlcat(msgbuf, "|", sizeof(msgbuf));
> strlcat(msgbuf, cmds[i], sizeof(msgbuf));
> }
not a big deal either way. but it's not like we're making an OS to
optimize the building of a message buffer. wait, maybe it's time to
grab www.msgbufos.org!
barret
--
You received this message because you are subscribed to the Google Groups
"Akaros" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
For more options, visit https://groups.google.com/d/optout.