commit 84f068238f70b690dfef63feffb4d3b1a03c3853
parent 623f4eaa26a92e740944174d3d5a363bd5562647
Author: vaplv <vaplv@free.fr>
Date: Sun, 29 Oct 2017 17:54:54 +0100
Fix the time_dump function and remove a zealous ASSERT
The size of the destination buffer was wrongly reduced of one char due
to a misinterpretation of the snprintf documentation.
Diffstat:
3 files changed, 16 insertions(+), 10 deletions(-)
diff --git a/src/clock_time.c b/src/clock_time.c
@@ -151,7 +151,7 @@ time_dump
char* dump,
size_t max_dump_len)
{
- size_t available_dump_space = max_dump_len ? max_dump_len - 1 : 0;
+ size_t available_dump_space = max_dump_len;
int64_t time_nsec = 0;
char* dst = dump;
@@ -173,7 +173,6 @@ time_dump
dst += len; \
available_dump_space -= (size_t)len; \
} else if(dst) { \
- dst[available_dump_space] = '\0'; \
available_dump_space = 0; \
dst = NULL; \
} \
@@ -226,8 +225,9 @@ time_dump
}
dump_len = strlen(dump);
}
- ASSERT(dump[dump_len-1] == ' ');
- dump[dump_len-1] = '\0';
+ if(dump[dump_len-1] == ' ') {
+ dump[dump_len-1] = '\0';
+ }
}
#undef DUMP
}
diff --git a/src/clock_time.h b/src/clock_time.h
@@ -71,7 +71,7 @@ time_val
RSYS_API void
time_dump
(const struct time* time,
- int flag, /* Combination of time_unit or TIME_DUMP_AUTO */
+ int flag, /* Combination of time_unit or TIME_ALL */
size_t* real_dump_len, /* May be NULL */
char* dump, /* May be NULL */
size_t max_dump_len);
diff --git a/src/test_time.c b/src/test_time.c
@@ -21,6 +21,7 @@ main(int argc, char** argv)
{
struct time start, end, res;
char dump[512];
+ char buf[32];
int64_t time = 0;
int64_t i = 0;
(void)argc, (void)argv;
@@ -38,14 +39,14 @@ main(int argc, char** argv)
time_dump
(&res, TIME_SEC|TIME_MSEC|TIME_USEC, NULL, dump, sizeof(dump));
- printf(">>> %s\n", dump);
+ printf(">>> %s.\n", dump);
time_dump(&res, TIME_ALL, NULL, dump, sizeof(dump));
- printf(">>> %s\n", dump);
+ printf(">>> %s.\n", dump);
CHECK(time_add(&res, &res, &res), &res);
CHECK(time_val(&res, TIME_NSEC), 2*time);
time_dump(&res, TIME_ALL, NULL, dump, sizeof(dump));
- printf(">>> %s\n", dump);
+ printf(">>> %s.\n", dump);
time = time_val(&res, TIME_NSEC);
CHECK(time_zero(&start), &start);
@@ -60,9 +61,14 @@ main(int argc, char** argv)
CHECK(time_sub(&res, &end, &end), &res);
CHECK(time_val(&res, TIME_NSEC), 0);
time_dump(&res, TIME_ALL, NULL, dump, sizeof(dump));
- printf(">>> %s\n", dump);
+ printf(">>> %s.\n", dump);
time_dump(&res, 0, NULL, dump, sizeof(dump));
- printf(">>> %s\n", dump);
+ printf(">>> %s.\n", dump);
+
+ res.sec = 5;
+ res.nsec = 524198207;
+ time_dump(&res, TIME_ALL, NULL, buf, sizeof(buf));
+ printf(">>> %s.\n", buf);
return 0;
}