作者chrisdar ()
看板C_and_CPP
標題[問題] 如何正確修正 warning C6011 ?
時間Sun Jul 29 22:18:40 2018
開發平台(Platform): (Ex: Win10, Linux, ...)
Windows 10 or 7
編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出)
Visual Studio Professional 2017 15.5.7
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
無,但是有啟動建置時啟用程式碼分析。
問題(Question):
已經寫了 null 指標檢查還是無法通過程式碼分析,
後來亂試試到一個騙過編譯器的方法如方法二,但是還是覺得這樣騙很不直覺。
想請問關於這個案例如何正確的解決C6011的改法。
餵入的資料(Input):
無。
預期的正確結果(Expected Output):
編譯成功,無警告。
錯誤結果(Wrong Output):
warning C6011: 正在取值 NULL 指標 'Ptr'。
程式碼(Code):(請善用置底文網頁, 記得排版,禁止使用圖檔)
#include <iostream>
static inline void *DummyPointerConvert (void *Ptr)
{
return Ptr;
}
#define WA_C6011(type,pointer) ((type *)(DummyPointerConvert (pointer)))
void Func()
{
for (uint8_t *Ptr = (uint8_t *) ( (long) 0xFE000);
Ptr < (uint8_t *) ( (long) 0x100000);
Ptr += 0x10) {
if ( (Ptr != NULL) && ( (* (uint32_t *) Ptr) == 0)) { // warning C6011
break;
}
if (*WA_C6011(uint32_t, Ptr) == 0) { // OK
break;
}
}
}
int main()
{
return 0;
}
補充說明(Supplement):
無。
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 106.1.10.98
※ 文章網址: https://webptt.com/m.aspx?n=bbs/C_and_CPP/M.1532873922.A.345.html
1F:推 eye5002003: #pragma warning(disable:6011)要是你相信自己的邏輯 07/29 22:56
2F:→ eye5002003: 對指標轉型挺危險的,要注意big/little-endian問題 07/29 22:59
3F:→ eye5002003: 以及memory alignment問題 07/29 23:01
4F:推 Sidney0503: 要寫C++就不要用C的東西 07/30 05:19
5F:→ Sidney0503: 該用nullptr不要用NULL, 指標轉型reinterpret_cast 07/30 05:25
6F:→ Sidney0503: 最後回答你的問題本身 因為你的寫法有讀取空標的風險 07/30 05:25
7F:→ Sidney0503: 所以會跳warning那行if要拆成巢狀兩層if 07/30 05:26
8F:→ Sidney0503: if(Ptr != nullptr){ 07/30 05:32
9F:→ Sidney0503: if((*(uint32_t *)Ptr) == 0){ 07/30 05:32
10F:→ Sidney0503: 指標沒指著東西本來就不該讀取 只是NULL幫你做了一層 07/30 05:34
11F:→ Sidney0503: 保險 不代表這個操作是正確的 07/30 05:35
1. 因為我寫iostream 害各位認為我在寫C++ 我在此更正為C, 是程式碼移植的失誤 抱歉
2. 我已經拆成巢狀 VS的程式碼分析還是報 C6011 在該位置
#include <stdint.h>
void Func()
{
for (uint8_t *Ptr = (uint8_t *) ( (long) 0xFE000);
Ptr < (uint8_t *) ( (long) 0x100000);
Ptr += 0x10) {
if (Ptr != nullptr) {
if ( (* (uint32_t *) Ptr) == 0) { // warning C6011
break;
}
}
}
}
int main()
{
return 0;
}
※ 編輯: chrisdar (106.1.10.98), 07/30/2018 05:48:04
13F:→ Sidney0503: 連MSDN都這樣寫...... 07/30 06:10
14F:→ Sidney0503: 你確定warning是那一行? 07/30 06:12
15F:→ Sidney0503: 你照MSDN的code打會出現warning嗎? 07/30 06:13
16F:→ Sidney0503: 不會是因為編譯時期決定的關係吧 因為你的程式不用 07/30 06:15
17F:→ Sidney0503: run-time就可以知道結果 所以編譯時期就看到了 07/30 06:15
18F:→ sharkbay: 跟 MSDN 寫的不同吧 這個有夾迴圈耶 07/30 06:33
19F:→ chrisdar: 是註解那一行沒錯, 測了幾種不同的迴圈都有一樣的C6011 07/30 08:36
20F:→ sarafciel: 你這個warning真正在警告的東西是那兩個直接塞的位置 07/30 10:09
21F:→ sarafciel: 0xFE000跟0x100000,如果真的要這樣寫而且你也確定這 07/30 10:11
22F:→ sarafciel: 兩個位置在你的機器上有效 那你就無視C6011吧 07/30 10:13
23F:→ sarafciel: 如果函數參數可以動 你把那兩個address在外面硬轉傳進 07/30 10:20
24F:→ sarafciel: 去Func 應該可以消這個warning 不過本質上也是騙就是XD 07/30 10:21
證實網友的看法:
#include <stdint.h>
void Func()
{
for (uint8_t *Ptr = (uint8_t *) ( (long) 0xFE000);
#if 1
true;
#else
Ptr < (uint8_t *) ( (long) 0x100000);
#endif
Ptr += 0x10) {
if ( (Ptr != nullptr) && (*Ptr == 0)) { // ok, 如果#if 0 C6011又出來
break;
}
}
}
int main()
{
return 0;
}
※ 編輯: chrisdar (106.1.10.98), 07/30/2018 20:15:44
另外一種騙法 給各位參考
#include <stdint.h>
static inline void *DummyPointerConvert (void *Ptr)
{
return Ptr;
}
#define WA_C6011(type,pointer) \
((type *)(DummyPointerConvert ((void *)(pointer))))
void Func()
{
for (uint8_t *Ptr = (uint8_t *) ( (long) 0xFE000);
Ptr < WA_C6011 (uint8_t, 0x100000);
Ptr += 0x10) {
if ( (Ptr != nullptr) && (*Ptr == 0)) { // OK
break;
}
}
}
int main()
{
return 0;
}
※ 編輯: chrisdar (106.1.10.98), 07/30/2018 20:21:24
※ 編輯: chrisdar (106.1.10.98), 07/30/2018 20:24:17
25F:推 sarafciel: 喔喔 原來只要終止條件有騙到就好XD 07/30 22:40