Skip to content
Browse files

msg: %-double prefix

All the existing callers use literal strings without any percent
characters, but we should protect against future callers including
a percent in the prefix string, too.
  • Loading branch information...
1 parent a40ffca commit fcb99cbe7715b5d8bb35184afe3178eaa79dcca3 @ChrisJohnsen committed Mar 24, 2011
Showing with 14 additions and 7 deletions.
  1. +14 −7 msg.c
View
21 msg.c
@@ -13,17 +13,21 @@ void vfmsg(FILE *f,
if (pre) prelen = strlen(pre);
if (fmt) fmtlen = strlen(fmt);
if (suf) suflen = strlen(suf);
- char *newfmt = malloc(prelen + fmtlen +
+ char *newfmt = malloc(
+ prelen*2 + /* %-doubled pre */
+ fmtlen +
2 + /* ':' and SP */
suflen*2 + /* %-doubled suf */
2 /* NL and NUL */
);
+ if (!newfmt)
+ goto finish;
char *newfmt_end = newfmt;
- if (prelen) {
- strcpy(newfmt_end, pre);
- newfmt_end += prelen;
- }
+ if (prelen)
+ while(*pre)
+ if ((*newfmt_end++ = *pre++) == '%')
+ *newfmt_end++ = '%';
if (fmtlen) {
strcpy(newfmt_end, fmt);
newfmt_end += fmtlen;
@@ -37,11 +41,14 @@ void vfmsg(FILE *f,
}
*newfmt_end++ = '\n';
*newfmt_end++ = '\0';
+ fmt = newfmt;
- vfprintf(f, newfmt, ap);
+finish:
+ vfprintf(f, fmt, ap);
fflush(f);
- free(newfmt);
+ if (newfmt)
+ free(newfmt);
}
FILE *msgout = NULL;

0 comments on commit fcb99cb

Please sign in to comment.
Something went wrong with that request. Please try again.