c - 下面的 Linux 驱动程序 C 代码是否有一个 off by one 错误?

标签 c

我正在阅读 USB Wi-Fi 卡的 C 驱动程序代码,遇到了我不确定自己是否完全理解的部分。我怀疑是我对 C 语言和运算符优先级的理解有误,驱动程序代码没问题,但我想检查一下。

/drivers/net/wireless/rtl818x/rtl8187/dev.c 中是一些代码,可将一堆值读取到 14 元素的 channels 数组中。 dev.c中的相关代码如下:

    channel = priv->channels;
    for (i = 0; i < 3; i++) {
            eeprom_93cx6_read(&eeprom, RTL8187_EEPROM_TXPWR_CHAN_1 + i,
                              &txpwr);
            (*channel++).hw_value = txpwr & 0xFF;
            (*channel++).hw_value = txpwr >> 8;
    }
    for (i = 0; i < 2; i++) {
            eeprom_93cx6_read(&eeprom, RTL8187_EEPROM_TXPWR_CHAN_4 + i,
                              &txpwr);
            (*channel++).hw_value = txpwr & 0xFF;
            (*channel++).hw_value = txpwr >> 8;
    }

    ....


    if (!priv->is_rtl8187b) {
            for (i = 0; i < 2; i++) {
                    eeprom_93cx6_read(&eeprom,
                                      RTL8187_EEPROM_TXPWR_CHAN_6 + i,
                                      &txpwr);
                    (*channel++).hw_value = txpwr & 0xFF;
                    (*channel++).hw_value = txpwr >> 8;
            }
    } else {
            eeprom_93cx6_read(&eeprom, RTL8187_EEPROM_TXPWR_CHAN_6,
                              &txpwr);
            (*channel++).hw_value = txpwr & 0xFF;

            eeprom_93cx6_read(&eeprom, 0x0A, &txpwr);
            (*channel++).hw_value = txpwr & 0xFF;

            eeprom_93cx6_read(&eeprom, 0x1C, &txpwr);
            (*channel++).hw_value = txpwr & 0xFF;
            (*channel++).hw_value = txpwr >> 8;
    }

我对这段代码的担心是,我认为第一次调用 (*channel++).hw_value = ... 会在 之前递增 channel 指针取消引用它,从而从 channel 的元素 [1] 开始并缺少元素 [0]。此外,无论执行了哪个 if/else 分支,我计算了 14 次对 (*channel++)... 的调用,所以我认为最后一次调用 (*channel++) 实际上会指向(不存在的)channel[15] 并覆盖堆栈中 channels 之后的任何变量的内存。谁能指出我的解释可能哪里出错了?

最佳答案

`*channel++`

解释: 1) *channel 即,存储在 channel 中的地址的值被处理。 2) channel 分号地址递增后。

以上是post增量的步骤。

因此,


    for(i=0;i

"CAN" simply mean if channel is already at position 0 (zero)

channel[0].hw_value =xyz; channel++;

关于c - 下面的 Linux 驱动程序 C 代码是否有一个 off by one 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12345427/

相关文章:

c++ - 如何检查指令是否为 LLVM IR 中的 PHI 指令

c - 将 C 指针代码示例转换为 Delphi 指针语法

c - 使用 SIGALRM 切换线程上下文

c++ - malloc 指针函数传递 fread

c - 实现插入排序时答案的差异

c - STM32f429ZI 无需调试器即可记录调用堆栈

c - C 宏的负面污名

c - 在 gcc 中初始化数组,未定义对 `memcpy' 的引用

C 将字符串传递给函数问题

c++ - "<<"(双尖括号)在 C/C++ 枚举中是什么意思?