錯誤: memcpy-param-overlap

位址清理程式錯誤:memcpy-param-overlap

CRT 函 memcpy式不支援 重迭的記憶體。 CRT 提供支援重迭記憶體的 memcpy 替代方案: memmove

常見的錯誤是將 視為 memmove 語意上相當於 memcpy

範例

// example1.cpp
// memcpy-param-overlap error
#include <string.h>

__declspec(noinline) void bad_function() {
    char buffer[] = "hello";

    memcpy(buffer, buffer + 1, 5); // BOOM!
}

int main(int argc, char **argv) {
    bad_function();
    return 0;
}

若要建置及測試此範例,請在Visual Studio 2019 16.9版或更新版本的 開發人員命令提示字元中執行下列命令:

cl example1.cpp /fsanitize=address /Zi /Oi
devenv /debugexe example1.exe

/Oi 旗標會指示編譯程式將 和 memmove 視為memcpy內部函數。 這是必要的,因為標準連結庫的某些版本會以相同的方式實 memcpy 作 和 memmove 。 因為 ASAN 是動態分析工具,所以只會偵測具有可觀察運行時間效果的錯誤。

產生的錯誤

顯示範例 1 中 memcpy-param-overlap 錯誤的調試程序螢幕快照。

另請參閱

AddressSanitizer 概觀
AddressSanitizer 已知問題
AddressSanitizer 組建和語言參考
AddressSanitizer 運行時間參考
AddressSanitizer 陰影位元組
AddressSanitizer 雲端或分散式測試
AddressSanitizer 調試程式整合
AddressSanitizer 錯誤範例