diff mbox series

[ovs-dev,4/4] general: Fix Clang's static analyzer 'Division by zero' warnings.

Message ID 169755059568.686004.15432881981934462506.stgit@ebuild
State Changes Requested
Headers show
Series Fix some of Clang's static analyzer warnings. | expand

Checks

Context Check Description
ovsrobot/apply-robot success apply and check: success
ovsrobot/github-robot-_Build_and_Test success github build: passed
ovsrobot/intel-ovs-compilation success test: success

Commit Message

Eelco Chaudron Oct. 17, 2023, 1:49 p.m. UTC
Signed-off-by: Eelco Chaudron <echaudro@redhat.com>
---
 lib/dpif-netdev.c       |    4 ++++
 tests/test-id-fpool.c   |    2 +-
 tests/test-mpsc-queue.c |    2 +-
 3 files changed, 6 insertions(+), 2 deletions(-)

Comments

Simon Horman Oct. 18, 2023, 11:02 a.m. UTC | #1
On Tue, Oct 17, 2023 at 03:49:55PM +0200, Eelco Chaudron wrote:

Hi Eelco,

As per my commoent on patch 3/4.
I think it would be good to be clear about if these are bugs or not.

> Signed-off-by: Eelco Chaudron <echaudro@redhat.com>
> ---
>  lib/dpif-netdev.c       |    4 ++++
>  tests/test-id-fpool.c   |    2 +-
>  tests/test-mpsc-queue.c |    2 +-
>  3 files changed, 6 insertions(+), 2 deletions(-)
> 
> diff --git a/lib/dpif-netdev.c b/lib/dpif-netdev.c
> index 157694bcf..b8f065d1d 100644
> --- a/lib/dpif-netdev.c
> +++ b/lib/dpif-netdev.c
> @@ -4748,6 +4748,10 @@ dpif_netdev_offload_stats_get(struct dpif *dpif,
>      }
>  
>      nb_thread = netdev_offload_thread_nb();
> +    if (!nb_thread) {
> +        return EINVAL;
> +    }
> +
>      /* nb_thread counters for the overall total as well. */
>      stats->size = ARRAY_SIZE(hwol_stats) * (nb_thread + 1);
>      stats->counters = xcalloc(stats->size, sizeof *stats->counters);
> diff --git a/tests/test-id-fpool.c b/tests/test-id-fpool.c
> index 27800aa9b..7bdb8154d 100644
> --- a/tests/test-id-fpool.c
> +++ b/tests/test-id-fpool.c
> @@ -237,7 +237,7 @@ print_result(const char *prefix)
>      for (i = 0; i < n_threads; i++) {
>          avg += thread_working_ms[i];
>      }
> -    avg /= n_threads;
> +    avg /= n_threads ? n_threads : 1;
>      printf("%s: ", prefix);
>      for (i = 0; i < n_threads; i++) {
>          if (thread_working_ms[i] >= TIMEOUT_MS) {
> diff --git a/tests/test-mpsc-queue.c b/tests/test-mpsc-queue.c
> index 16aa804a0..86a223caf 100644
> --- a/tests/test-mpsc-queue.c
> +++ b/tests/test-mpsc-queue.c
> @@ -315,7 +315,7 @@ print_result(const char *prefix, int reader_elapsed)
>      for (i = 0; i < n_threads; i++) {
>          avg += thread_working_ms[i];
>      }
> -    avg /= n_threads;
> +    avg /= n_threads ? n_threads : 1;
>      printf("%s:  %6d", prefix, reader_elapsed);
>      for (i = 0; i < n_threads; i++) {
>          printf(" %6" PRIu64, thread_working_ms[i]);
> 
> _______________________________________________
> dev mailing list
> dev@openvswitch.org
> https://mail.openvswitch.org/mailman/listinfo/ovs-dev
>
Eelco Chaudron Oct. 18, 2023, 11:13 a.m. UTC | #2
On 18 Oct 2023, at 13:02, Simon Horman wrote:

> On Tue, Oct 17, 2023 at 03:49:55PM +0200, Eelco Chaudron wrote:
>
> Hi Eelco,
>
> As per my commoent on patch 3/4.
> I think it would be good to be clear about if these are bugs or not.

Dont think this will happen in real life, just to avoid clang warning. Will make this clear in the commit message.

Will send out a v2 next week to give other people time to react.

//Eelco

>> Signed-off-by: Eelco Chaudron <echaudro@redhat.com>
>> ---
>>  lib/dpif-netdev.c       |    4 ++++
>>  tests/test-id-fpool.c   |    2 +-
>>  tests/test-mpsc-queue.c |    2 +-
>>  3 files changed, 6 insertions(+), 2 deletions(-)
>>
>> diff --git a/lib/dpif-netdev.c b/lib/dpif-netdev.c
>> index 157694bcf..b8f065d1d 100644
>> --- a/lib/dpif-netdev.c
>> +++ b/lib/dpif-netdev.c
>> @@ -4748,6 +4748,10 @@ dpif_netdev_offload_stats_get(struct dpif *dpif,
>>      }
>>
>>      nb_thread = netdev_offload_thread_nb();
>> +    if (!nb_thread) {
>> +        return EINVAL;
>> +    }
>> +
>>      /* nb_thread counters for the overall total as well. */
>>      stats->size = ARRAY_SIZE(hwol_stats) * (nb_thread + 1);
>>      stats->counters = xcalloc(stats->size, sizeof *stats->counters);
>> diff --git a/tests/test-id-fpool.c b/tests/test-id-fpool.c
>> index 27800aa9b..7bdb8154d 100644
>> --- a/tests/test-id-fpool.c
>> +++ b/tests/test-id-fpool.c
>> @@ -237,7 +237,7 @@ print_result(const char *prefix)
>>      for (i = 0; i < n_threads; i++) {
>>          avg += thread_working_ms[i];
>>      }
>> -    avg /= n_threads;
>> +    avg /= n_threads ? n_threads : 1;
>>      printf("%s: ", prefix);
>>      for (i = 0; i < n_threads; i++) {
>>          if (thread_working_ms[i] >= TIMEOUT_MS) {
>> diff --git a/tests/test-mpsc-queue.c b/tests/test-mpsc-queue.c
>> index 16aa804a0..86a223caf 100644
>> --- a/tests/test-mpsc-queue.c
>> +++ b/tests/test-mpsc-queue.c
>> @@ -315,7 +315,7 @@ print_result(const char *prefix, int reader_elapsed)
>>      for (i = 0; i < n_threads; i++) {
>>          avg += thread_working_ms[i];
>>      }
>> -    avg /= n_threads;
>> +    avg /= n_threads ? n_threads : 1;
>>      printf("%s:  %6d", prefix, reader_elapsed);
>>      for (i = 0; i < n_threads; i++) {
>>          printf(" %6" PRIu64, thread_working_ms[i]);
>>
>> _______________________________________________
>> dev mailing list
>> dev@openvswitch.org
>> https://mail.openvswitch.org/mailman/listinfo/ovs-dev
>>
diff mbox series

Patch

diff --git a/lib/dpif-netdev.c b/lib/dpif-netdev.c
index 157694bcf..b8f065d1d 100644
--- a/lib/dpif-netdev.c
+++ b/lib/dpif-netdev.c
@@ -4748,6 +4748,10 @@  dpif_netdev_offload_stats_get(struct dpif *dpif,
     }
 
     nb_thread = netdev_offload_thread_nb();
+    if (!nb_thread) {
+        return EINVAL;
+    }
+
     /* nb_thread counters for the overall total as well. */
     stats->size = ARRAY_SIZE(hwol_stats) * (nb_thread + 1);
     stats->counters = xcalloc(stats->size, sizeof *stats->counters);
diff --git a/tests/test-id-fpool.c b/tests/test-id-fpool.c
index 27800aa9b..7bdb8154d 100644
--- a/tests/test-id-fpool.c
+++ b/tests/test-id-fpool.c
@@ -237,7 +237,7 @@  print_result(const char *prefix)
     for (i = 0; i < n_threads; i++) {
         avg += thread_working_ms[i];
     }
-    avg /= n_threads;
+    avg /= n_threads ? n_threads : 1;
     printf("%s: ", prefix);
     for (i = 0; i < n_threads; i++) {
         if (thread_working_ms[i] >= TIMEOUT_MS) {
diff --git a/tests/test-mpsc-queue.c b/tests/test-mpsc-queue.c
index 16aa804a0..86a223caf 100644
--- a/tests/test-mpsc-queue.c
+++ b/tests/test-mpsc-queue.c
@@ -315,7 +315,7 @@  print_result(const char *prefix, int reader_elapsed)
     for (i = 0; i < n_threads; i++) {
         avg += thread_working_ms[i];
     }
-    avg /= n_threads;
+    avg /= n_threads ? n_threads : 1;
     printf("%s:  %6d", prefix, reader_elapsed);
     for (i = 0; i < n_threads; i++) {
         printf(" %6" PRIu64, thread_working_ms[i]);