- for (; addr && buflen > 0; addr = addr->next) {
- /* use buflen+1 here because we already saved space for the trailing
- nul char, and the subroutine can make use of it */
- rfc822_write_address_single (pbuf, buflen + 1, addr, display);
-
- /* this should be safe since we always have at least 1 char passed into
- the above call, which means `pbuf' should always be nul terminated */
- len = m_strlen(pbuf);
- pbuf += len;
- buflen -= len;
-
- /* if there is another address, and its not a group mailbox name or
- group terminator, add a comma to separate the addresses */
- if (addr->next && addr->next->mailbox && !addr->group) {
- if (!buflen)
- goto done;
- *pbuf++ = ',';
- buflen--;
- if (!buflen)
- goto done;
- *pbuf++ = ' ';
- buflen--;
+ for (; addr; addr = addr->next) {
+ pos += rfc822_write_address_single(buf + pos, buflen + 1 - pos,
+ addr, display);
+
+ if (!addr->group && addr->next && addr->next->mailbox) {
+ /* if there is another address, and its not a group mailbox name or
+ group terminator, add a comma to separate the addresses */
+ if (pos + 2 >= buflen)
+ break;
+
+ buf[pos++] = ',';
+ buf[pos++] = ' ';