tools: address recently reported Coverity issues
cover tools: address recently reported Coverity issues
Three of the four reported issues a pretty clear how to take care of, which is done in this series. The 4th, CID 1509375, isn't as obvious, mainly because I don't know enough about Coverity to be able to tell whether adding a cast there might help; the issue reported for xenbaked suggests it wouldn't. The alternative I can see would be an arch-independent piece of inline assembly, hiding the transformation from time_t to int from the analysis tool. But I'm not convinced such would be liked. Nor am I sure the can be fully arch-ignorant, as time_t potentially being a 64-bit type might not be possible to match with an "r" constraint on all 32- bit architectures. So maybe what's left is memcpy() from a "time_t" variable to an "int" one ...
1: xenbaked: properly use time_t in dump_stats()
2: console/client: properly use time_t in get_pty_fd() 3: libxl: use time_t for qmp_synchronous_send()'s last parameter
Jan
PATCH 1/3 xenbaked: properly use time_t in dump_stats()
"int" is not a suitable type to convert time()'s return value to. Avoid casts and other extra fiddling by using difftime(), on the assumption that the overhead of using "double" doesn't matter here.
Coverity ID: 1509374
tools/xenmon/xenbaked.c +5 -11
@@ -230,11 +230,7 @@ static void check_gotten_sum(void)static void dump_stats (void ) {stat_map_t *smt = stat_map; -time_t end_time, run_time; - -time (&end_time); - - run_time = end_time - start_time; +double run_time =difftime (time (NULL ), start_time);printf ("Event counts:\n" );while (smt->text !=NULL ) {
@@ -242,13 +238,11 @@ static void dump_stats(void) smt++; } -printf ("processed %d total records in %d seconds (%ld per second)\n" , - rec_count, (int )run_time, - run_time ? (long )(rec_count/run_time) :0L ); +printf ("processed %d total records in %0f seconds (%0f per second)\n" , + rec_count, run_time, run_time ? rec_count / run_time :0 ); -printf ("woke up %d times in %d seconds (%ld per second)\n" , - wakeups, (int ) run_time, - run_time ? (long )(wakeups/run_time) :0L ); +printf ("woke up %d times in %0f seconds (%0f per second)\n" , + wakeups, run_time, run_time ? wakeups / run_time :0 );check_gotten_sum (); }
Re: [PATCH 1/3] xenbaked: properly use time_t in dump_stats()
Quoted 3 lines · Jan Beulich
"int" is not a suitable type to convert time()'s return value to. Avoid casts and other extra fiddling by using
difftime(), on the assumption that the overhead of using "double" doesn't matter here.
dump_stats() seems to be only used once, so it's probably fine.
Quoted 19 lines
Coverity ID: 1509374
Signed-off-by Jan Beulich <[email protected]>tools/xenmon/xenbaked.c +1
-5@@ -230,11 +230,7 @@ static void check_gotten_sum(void)static void dump_stats (void ) {stat_map_t *smt = stat_map; -time_t end_time, run_time; - -time (&end_time); - - run_time = end_time - start_time; +double run_time =difftime (time (NULL ), start_time);printf ("Event counts:\n" );while (smt->text !=NULL ) {
tools/xenmon/xenbaked.c static void dump_stats(void) · line 241
Quoted 7 lines
tools/xenmon/xenbaked.c +1
-3@@ -242,13 +238,11 @@ static void dump_stats(void) smt++; } -printf ("processed %d total records in %d seconds (%ld per second)\n" , - rec_count, (int )run_time, - run_time ? (long )(rec_count/run_time) :0L ); + printf("processed %d total records in %0f seconds (%0f per second)\n",
Did you want to write "%.0f" or even "%.f" instead of "%0f"? It seems that the '0' here mean '0'-padded, but without field width there would be nothing to pad. I'm guessing you would want printf to write something like "266 seconds" instead of "266.646168 seconds". Am I missing something ?
Quoted 11 lines
tools/xenmon/xenbaked.c +3
-3+ rec_count, run_time, run_time ? rec_count / run_time : 0); -printf ("woke up %d times in %d seconds (%ld per second)\n" , - wakeups, (int ) run_time, - run_time ? (long )(wakeups/run_time) :0L ); +printf ("woke up %d times in %0f seconds (%0f per second)\n" , + wakeups, run_time, run_time ? wakeups / run_time :0 );check_gotten_sum (); }
Signature · 1 line
Anthony PERARD
Re: [PATCH 1/3] xenbaked: properly use time_t in dump_stats()
Quoted 5 lines · Anthony PERARD
Quoted 4 lines · Jan Beulich
(quoted diff) +0
-0@@ -242,13 +238,11 @@ static void dump_stats(void) smt++; }
Quoted 10 lines
tools/xenmon/xenbaked.c static void dump_stats(void) · line 241
Quoted 4 lines
tools/xenmon/xenbaked.c +1
-3-printf ("processed %d total records in %d seconds (%ld per second)\n" , - rec_count, (int )run_time, - run_time ? (long )(rec_count/run_time) :0L ); + printf("processed %d total records in %0f seconds (%0f per second)\n",Did you want to write "%.0f" or even "%.f" instead of "%0f"? It seems that the '0' here mean '0'-padded, but without field width there would be nothing to pad. I'm guessing you would want printf to write something like "266 seconds" instead of "266.646168 seconds". Am I missing something ?
Indeed, I did miss the . in all cases. How embarrassing ...
Jan
PATCH 2/3 console/client: properly use time_t in get_pty_fd()
In reply to Jan Beulich"int" is not a suitable type to hold time()'s return value.
Coverity ID: 1509376
---
I was on the edge of switching to using difftime() at this occasion as well. This would then also allow switching "seconds" to unsigned int (or any other suitable unsigned type).
tools/console/client/main.c +1 -1
@@ -102,7 +102,7 @@ static int get_pty_fd(struct xs_handle *struct timeval tv;fd_set watch_fdset;int xs_fd =xs_fileno (xs), pty_fd =-1 ; -int start, now; +time_t start, now;unsigned int len =0 ;char *pty_path, **watch_paths;
Re: [PATCH 2/3] console/client: properly use time_t in get_pty_fd()
Quoted 4 lines · Jan Beulich
"int" is not a suitable type to hold time()'s return value.
Coverity ID: 1509376
Signed-off-by Jan Beulich <[email protected]>
Thanks,
Signature · 1 line
Anthony PERARD
PATCH 3/3 libxl: use time_t for qmp_synchronous_send()'s last parameter
In reply to Jan Beulich"int" is not a suitable type to hold / receive "time_t" values.
The parameter is presently unused, so no functional change.
Coverity ID: 1509377
---
An obvious alternative would be to drop the parameter for being unused, but I assume there were plans to use it in some way.
tools/libs/light/libxl_qmp.c +1 -1
@@ -582,7 +582,7 @@ out:static int qmp_synchronous_send (libxl__qmp_handler *qmp,const char *cmd,libxl__json_object *args,qmp_callback_t callback,void *opaque, -int ask_timeout) +time_t ask_timeout) {int id =0 ;int ret =0 ;
Re: [PATCH 3/3] libxl: use time_t for qmp_synchronous_send()'s last parameter
Quoted 6 lines · Jan Beulich
"int" is not a suitable type to hold / receive "
time_t" values.The parameter is presently unused, so no functional change.
Coverity ID: 1509377
Signed-off-by Jan Beulich <[email protected]>
The severity of this issue is rather low IMO. A timeout of more than 60 years not being handled correctly seems to have no relevance at all.
Juergen
Re: [PATCH 3/3] libxl: use time_t for qmp_synchronous_send()'s last parameter
Quoted 9 lines · Juergen Gross
Quoted 6 lines · Jan Beulich
"int" is not a suitable type to hold / receive "
time_t" values.The parameter is presently unused, so no functional change.
Coverity ID: 1509377
Signed-off-by Jan Beulich <[email protected]>Reviewed-by Juergen Gross <[email protected]>
Thanks.
Quoted 2 lines
The severity of this issue is rather low IMO. A timeout of more than 60 years not being handled correctly seems to have no relevance at all.
Agreed. The tool can't tell that a time_t-type value is used here for a timeout, not a time stamp, and using the correct type is Generally Better™ anyway, imo.
Jan
Re: [PATCH 3/3] libxl: use time_t for qmp_synchronous_send()'s last parameter
In reply to Jan BeulichQuoted 6 lines · Jan Beulich
"int" is not a suitable type to hold / receive "
time_t" values.The parameter is presently unused, so no functional change.
Coverity ID: 1509377
Signed-off-by Jan Beulich <[email protected]>
Quoted 3 lines
---
An obvious alternative would be to drop the parameter for being unused, but I assume there were plans to use it in some way.
I'd rather drop the function all together instead, it doesn't get much use these days as it's been replaced in many cases.
On the other hand, there seems to be only one call site, it would be only one extra change to drop it, so that would be one way to fix this. As you wish.
Thanks,
Signature · 1 line
Anthony PERARD
PATCH v2 1/1 xenbaked: properly use time_t in dump_stats()
In reply to Jan Beulich"int" is not a suitable type to convert time()'s return value to. Avoid casts and other extra fiddling by using difftime(), on the assumption that the overhead of using "double" doesn't matter here.
Coverity ID: 1509374
---
v2: Properly use %.0f everywhere.
tools/xenmon/xenbaked.c +5 -11
@@ -230,11 +230,7 @@ static void check_gotten_sum(void)static void dump_stats (void ) {stat_map_t *smt = stat_map; -time_t end_time, run_time; - -time (&end_time); - - run_time = end_time - start_time; +double run_time =difftime (time (NULL ), start_time);printf ("Event counts:\n" );while (smt->text !=NULL ) {
@@ -242,13 +238,11 @@ static void dump_stats(void) smt++; } -printf ("processed %d total records in %d seconds (%ld per second)\n" , - rec_count, (int )run_time, - run_time ? (long )(rec_count/run_time) :0L ); +printf ("processed %d total records in %.0f seconds (%.0f per second)\n" , + rec_count, run_time, run_time ? rec_count / run_time :0 ); -printf ("woke up %d times in %d seconds (%ld per second)\n" , - wakeups, (int ) run_time, - run_time ? (long )(wakeups/run_time) :0L ); +printf ("woke up %d times in %.0f seconds (%.0f per second)\n" , + wakeups, run_time, run_time ? wakeups / run_time :0 );check_gotten_sum (); }
Re: [PATCH v2] xenbaked: properly use time_t in dump_stats()
Quoted 8 lines · Jan Beulich
"int" is not a suitable type to convert time()'s return value to. Avoid casts and other extra fiddling by using
difftime(), on the assumption that the overhead of using "double" doesn't matter here.Coverity ID: 1509374
Signed-off-by Jan Beulich <[email protected]>---
v2: Properly use %.0f everywhere.
Thanks,
Signature · 1 line
Anthony PERARD