diff options
-rw-r--r-- | fref.lex | 14 |
1 files changed, 8 insertions, 6 deletions
@@ -157,20 +157,22 @@ harvard() CL(e.xx); } -/* Print formatted text if given strings are non-empty. */ +/* Print formatted text if given fields are non-empty. */ int pf(char *fmt, ...) { char *buf, *p; - int n; + int n, sz; va_list ap; - n = 0; - for(p = fmt; *p; p++) + /* Count given fields. */ + for(n = 0, p = fmt; *p; p++) if(*p == '%') n++; - if(!(buf = malloc(strlen(fmt)+n*MAX+1))) + /* Allocate enough memory to fit the given fields. */ + sz = strlen(fmt)+n*MAX+1; + if(!(buf = malloc(sz))) err(1, "malloc"); va_start(ap, fmt); @@ -180,7 +182,7 @@ pf(char *fmt, ...) p = va_arg(ap, char *); if(!p || !*p) return 0; - strcat(buf, p); + strncat(buf, p, sz-1); break; default: strncat(buf, fmt, 1); |