# DBZ.ITERATOR

## 在除以零的运算中可能使用了循环迭代器

DBZ 检查器会查找零常量值用作除法或模运算除数的实例。

DBZ.ITERATOR 检查器会标记以下情况：循环迭代器在循环执行中被赋值为零常数值，而该循环迭代器后续在不进行零值检查的情况下，可能被显式使用或传递到可能将其用作除法或模运算除数的函数。

## 漏洞代码示例

```1
2
3
4
5
6
7
8
`  int count_sum_of_custom_arithmetic_series()  {          int sum = 0;      for (int i = -10; i < 10; ++i) {          sum += 3628800 / i;      }          return sum;  }````

Klocwork 针对第 5 行生成问题报告，指出在循环迭代期间循环迭代器 i 被赋值为零常量值，并且该值被用作除法运算的除数。除以零会产生无法预料的意外结果。

## 修正代码示例

```1
2
3
4
5
6
7
8
9
10
`  int count_sum_of_custom_arithmetic_series()  {          int sum = 0;      for (int i = -10; i < 10; ++i) {          if (i != 0) {              sum += 3628800 / i;          }      }      return sum;  }````

## 限制

```1
2
3
4
5
6
7
8
`  int count_sum_of_custom_arithmetic_series()  {      int sum = 0;      for (int i = -10; i < 10; ++i) {          sum += 3628800 / (i - i);   // Divide by zero will happen here since i - i = 0.Not detected by this checker.      }      return sum;  }````

```1
2
3
4
5
6
7
8
`  intcount_sum_of_custom_arithmetic_series_with_step_of_two()  {      int sum = 0;      for (int i = -10; i < 10; i += 2) {          sum += 3628800 / i;            // Divide by zero will not be detected here since it the step of i is +2.      }      return sum;  }````