Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

变量是个端口或者对应寄存器的话只写入是没问题的 #25

Closed
onionblack opened this issue Feb 10, 2023 · 3 comments
Closed

Comments

@onionblack
Copy link

safe-rules/c-cpp-rules.md

Lines 10740 to 10744 in d06e2da

对于内存中的数据,写入之后应被读取,如果出现:
1. 写入后未经读取再次被无条件写入
2. 写入后未经读取即结束了函数的执行
这种写入是无效的,出现这种问题往往意味着逻辑错误或功能不完整。

我的建议是补充说明一下

@Timothy-Liuxf
Copy link

Timothy-Liuxf commented Feb 10, 2023

这种情况对象应该声明为 volatile,应该属于第 10746 行和第 6507 行说明的情况吧。

safe-rules/c-cpp-rules.md

Lines 10740 to 10746 in d06e2da

对于内存中的数据,写入之后应被读取,如果出现:
1. 写入后未经读取再次被无条件写入
2. 写入后未经读取即结束了函数的执行
这种写入是无效的,出现这种问题往往意味着逻辑错误或功能不完整。
对象在初始化时的写入和 volatile 型数据可不受本规则限制。

safe-rules/c-cpp-rules.md

Lines 6502 to 6510 in d06e2da

应在适当的场景中合理使用 volatile,否则会导致优化或同步相关的多种问题。
只应在以下场景使用 volatile:
- 与汇编等语言混合处理同一对象
- 信号或中断处理函数处理共享对象
- 对象地址对应外设地址
- 出于安全目的清理内存中的对象
在这些场景中,如果相关对象没有用 volatile 限定,会导致程序和预期不符,volatile 关键字可以保证对象具有稳定的内存地址,任何读取或写入都可以来源于或作用于内存中的实际数据。

@onionblack
Copy link
Author

也是,那我先关了,我们有一段代码是这样的

while (1) {
  port = 0;
  port = 1;
}

不过port是个对象等号是重载的

@brotherbeer
Copy link
Collaborator

嗯嗯,可以强调一下“存在写入数据之外的副作用时,可不受本规则约束”

brotherbeer added a commit that referenced this issue Feb 14, 2023
对 ID_invalidWrite 强调特殊副作用可不受约束 ( #25 )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants