if srclen>maxlen, truncate at maxlen

master
leitner 11 years ago
parent a85bfc1b81
commit 21d0ea4549

@ -8,7 +8,8 @@ size_t fmt_pad(char* dest,const char* src,size_t srclen,size_t padlen,size_t max
long todo; long todo;
char* olddest=dest; char* olddest=dest;
char* max=dest+maxlen; char* max=dest+maxlen;
todo=padlen-srclen; if ((long)srclen<0 || (long)padlen<0 || (long)maxlen<0) return 0;
todo=(long)(padlen-srclen);
if (dest==0) { if (dest==0) {
unsigned long sum=srclen>padlen?srclen:padlen; unsigned long sum=srclen>padlen?srclen:padlen;
return sum>maxlen?maxlen:sum; return sum>maxlen?maxlen:sum;
@ -17,9 +18,9 @@ size_t fmt_pad(char* dest,const char* src,size_t srclen,size_t padlen,size_t max
if (dest>max) break; if (dest>max) break;
*dest=' '; ++dest; *dest=' '; ++dest;
} }
for (todo=srclen; todo>0; --todo) { for (todo=(long)srclen>maxlen?maxlen:srclen; todo>0; --todo) {
if (dest>max) break; if (dest>max) break;
*dest=*src; ++dest; ++src; *dest=*src; ++dest; ++src;
} }
return dest-olddest; return (size_t)(dest-olddest);
} }

Loading…
Cancel
Save