From 78e5e11b1a3d60026021b8ae44ef1fb1b540661e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Love=20H=C3=B6rnquist=20=C3=85strand?= Date: Sat, 30 Oct 2004 22:33:08 +0000 Subject: [PATCH] Change the behavior of the parse_unit code to return the number of bytes needed to print the whole string (minus the trailing '\0'), just like snprintf. Idea from bugreport from Gabriel Kihlman . git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@14324 ec53bebd-3082-4978-b11e-865c3cabbd6b --- lib/roken/parse_time.3 | 6 +++++- lib/roken/parse_units.c | 10 +++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/roken/parse_time.3 b/lib/roken/parse_time.3 index fbcce903b..d824e7691 100644 --- a/lib/roken/parse_time.3 +++ b/lib/roken/parse_time.3 @@ -107,7 +107,11 @@ and .Fn unparse_time_approx return the number of characters written to .Fa buf . -.\".Sh EXAMPLES +if the return value is greater than or equal to the +.Fa len +argument, the string was too short and some of the printed characters +were discarded. +.Sh EXAMPLES .Bd -literal #include #include diff --git a/lib/roken/parse_units.c b/lib/roken/parse_units.c index f224814eb..d417fe23d 100644 --- a/lib/roken/parse_units.c +++ b/lib/roken/parse_units.c @@ -212,9 +212,13 @@ unparse_something (int num, const struct units *units, char *s, size_t len, tmp = (*print) (s, len, div, u->name, num); if (tmp < 0) return tmp; - - len -= tmp; - s += tmp; + if (tmp > len) { + len = 0; + s = NULL; + } else { + len -= tmp; + s += tmp; + } ret += tmp; } }