From 9b6671efeec6fe3bca1492fc2e893ebcb9de0a34 Mon Sep 17 00:00:00 2001 From: leitner Date: Sat, 15 Mar 2014 15:18:55 +0000 Subject: [PATCH] add buffer_tosa check to marshal.c failed attempt to improve scan_ulongn, results in essentially same code but the C code looks slighly better so I'm keeping it --- scan/scan_ulongn.c | 6 ++---- test/marshal.c | 10 ++++++++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/scan/scan_ulongn.c b/scan/scan_ulongn.c index 4011fb4..b519811 100644 --- a/scan/scan_ulongn.c +++ b/scan/scan_ulongn.c @@ -14,10 +14,8 @@ size_t scan_ulongn(const char* src,size_t n,unsigned long int* dest) { * so instead of *10 we do (l<<3) (i.e. *8) + (l<<1) (i.e. *2) * and check for overflow on all the intermediate steps */ n=l<<3; if ((n>>3)!=l) break; - if (n+(l<<1) < n) break; - n+=l<<1; - if (n+c < n) break; - l=n+c; + if (n+(l<<1)+c < n) break; + l=n+(l<<1)+c; ++tmp; } if (tmp-src) *dest=l; diff --git a/test/marshal.c b/test/marshal.c index 95b22cb..c559ea3 100644 --- a/test/marshal.c +++ b/test/marshal.c @@ -398,12 +398,22 @@ int main() { { buffer b; stralloc s; + size_t i; buffer_fromsa(&b,&sa); stralloc_init(&s); assert(buffer_getline_sa(&b,&s)==1 && stralloc_equals(&s,"foo\n")); assert(buffer_getnewline_sa(&b,&s)==1 && stralloc_equals(&s,"bar\r\n")); assert(buffer_getnewline_sa(&b,&s)==0 && stralloc_equals(&s,"baz")); buffer_close(&b); + + stralloc_free(&s); + buffer_tosa(&b,&s); + for (i=0; i<100; ++i) + buffer_puts(&b,"foo bar baz!\n"); + buffer_flush(&b); + assert(s.len==100*sizeof("foo bar baz!")); + for (i=0; i<100; ++i) + assert(byte_equal(s.s+i*sizeof("foo bar baz!"),sizeof("foo bar baz!"),"foo bar baz!\n")); } return 0;