【问题标题】:Access violation executing location - GetFileAttributesA访问冲突执行位置 - GetFileAttributesA
【发布时间】:2014-04-09 11:28:15
【问题描述】:

我在 VS2012 上的 Windows 8.1 x64 上遇到了 GetFileAttributedA 的特殊问题。函数运行良好,但有时语句会因

而崩溃

MyService.exe 中 0x00000003 处未处理的异常:0xC0000005:访问冲突执行位置 0x00000003。

这是伪代码。

char    szItemAbsPath[MAX_PATH];
DWORD   rc = 0;
DWORD   dwFileAttribute = 0;

memset(szItemAbsPath, '\0', sizeof(szItemAbsPath));
sprintf(szItemAbsPath, "%s%s%s", "D:\\Root", FILE_SEPARATOR_STR, "temp.txt");

// check item type
dwFileAttribute = GetFileAttributesA(szItemAbsPath); //this is where program crashes
if (dwFileAttribute == INVALID_FILE_ATTRIBUTES)
{
    rc = GetLastError();
        return rc;
}

if (dwFileAttribute & FILE_ATTRIBUTE_DIRECTORY)
{
    // this is a dir
}

我基本上想检查给定项目是文件还是文件夹。这个问题有时也出现在 32 位版本中。

编辑:

迷你转储

Dump Summary
------------
Dump File:  MyDesktopService.dmp : C:\Users\anand\Desktop\MyDesktopService.dmp
Last Write Time:    09-04-2014 17:34:24
Process Name:   MyDesktopService.exe : D:\Works\My\My_Recovered_from_SVN_Crash\MyVS2012\NSLib\MyDesktopService.exe
Process Architecture:   x86
Exception Code: 0xC0000005
Exception Information:  The thread tried to read from or write to a virtual address for which it does not have the appropriate access.
Heap Information:   Present

System Information
------------------
OS Version: 6.2.9200
CLR Version(s): 

Modules
-------
Module Name Module Path Module Version
----------- ----------- --------------
MyDesktopService.exe    D:\Works\My\My_Recovered_from_SVN_Crash\MyVS2012\NSLib\MyDesktopService.exe 0.0.0.0
ntdll.dll   C:\Windows\System32\ntdll.dll   6.3.9600.16502
kernel32.dll    C:\Windows\System32\kernel32.dll    6.3.9600.16520
KERNELBASE.dll  C:\Windows\System32\KERNELBASE.dll  6.3.9600.16496
MSVCR110D.dll   D:\Works\My\My_Recovered_from_SVN_Crash\MyVS2012\NSLib\MSVCR110D.dll    11.0.51106.1
MyDesktop.dll   D:\Works\My\My_Recovered_from_SVN_Crash\MyVS2012\NSLib\MyDesktop.dll    0.0.0.0
libeay32.dll    D:\Works\My\My_Recovered_from_SVN_Crash\MyVS2012\NSLib\libeay32.dll 1.0.1.6
wtsapi32.dll    C:\Windows\System32\wtsapi32.dll    6.3.9600.16384
userenv.dll C:\Windows\System32\userenv.dll 6.3.9600.16384
advapi32.dll    C:\Windows\System32\advapi32.dll    6.3.9600.16384
ws2_32.dll  C:\Windows\System32\ws2_32.dll  6.3.9600.16384
libcurl_debug.dll   D:\Works\My\My_Recovered_from_SVN_Crash\MyVS2012\NSLib\libcurl_debug.dll    7.34.0.0
jansson.dll D:\Works\My\My_Recovered_from_SVN_Crash\MyVS2012\NSLib\jansson.dll  0.0.0.0
gdi32.dll   C:\Windows\System32\gdi32.dll   6.3.9600.16421
user32.dll  C:\Windows\System32\user32.dll  6.3.9600.16441
msvcr110.dll    C:\Windows\System32\msvcr110.dll    11.0.51106.1
msvcrt.dll  C:\Windows\System32\msvcrt.dll  7.0.9600.16384
rpcrt4.dll  C:\Windows\System32\rpcrt4.dll  6.3.9600.16384
profapi.dll C:\Windows\System32\profapi.dll 6.3.9600.16384
sechost.dll C:\Windows\System32\sechost.dll 6.3.9600.16384
nsi.dll C:\Windows\System32\nsi.dll 6.3.9600.16384
Wldap32.dll C:\Windows\System32\Wldap32.dll 6.3.9600.16384
normaliz.dll    C:\Windows\System32\normaliz.dll    6.3.9600.16384
ssleay32.dll    D:\Works\My\My_Recovered_from_SVN_Crash\MyVS2012\NSLib\ssleay32.dll 1.0.1.6
sspicli.dll C:\Windows\System32\sspicli.dll 6.3.9600.16408
CRYPTBASE.dll   C:\Windows\System32\CRYPTBASE.dll   6.3.9600.16384
bcryptPrimitives.dll    C:\Windows\System32\bcryptPrimitives.dll    6.3.9600.16384
winsta.dll  C:\Windows\System32\winsta.dll  6.3.9600.16384
IPHLPAPI.DLL    C:\Windows\System32\IPHLPAPI.DLL    6.3.9600.16384
winnsi.dll  C:\Windows\System32\winnsi.dll  6.3.9600.16384
dhcpcsvc6.DLL   C:\Windows\System32\dhcpcsvc6.DLL   6.3.9600.16384
dhcpcsvc.dll    C:\Windows\System32\dhcpcsvc.dll    6.3.9600.16384
mswsock.dll C:\Windows\System32\mswsock.dll 6.3.9600.16384
dnsapi.dll  C:\Windows\System32\dnsapi.dll  6.3.9600.16423
rasadhlp.dll    C:\Windows\System32\rasadhlp.dll    6.3.9600.16384
NapiNSP.dll C:\Windows\System32\NapiNSP.dll 6.3.9600.16384
pnrpnsp.dll C:\Windows\System32\pnrpnsp.dll 6.3.9600.16384
nlaapi.dll  C:\Windows\System32\nlaapi.dll  6.3.9600.16384
winrnr.dll  C:\Windows\System32\winrnr.dll  6.3.9600.16384
wshbth.dll  C:\Windows\System32\wshbth.dll  6.3.9600.16384
cryptsp.dll C:\Windows\System32\cryptsp.dll 6.3.9600.16384
rsaenh.dll  C:\Windows\System32\rsaenh.dll  6.3.9600.16384
bcrypt.dll  C:\Windows\System32\bcrypt.dll  6.3.9600.16384

调用栈:

    00000003()  Unknown
    [Frames below may be incorrect and/or missing]  
>   MyDesktop.dll!HandleModifyEvent(...) Line 159   C
    MyDesktop.dll!ProcessFSEvent(...) Line 79   C
    MyDesktop.dll!NSFileSysEventProcessor(void * lpParam=0x01adf324) Line 59    C
    MyDesktop.dll!thread_first_breath(sThrdInfo * psParam=0x009d2848) Line 1119 C
    msvcr110d.dll!_callthreadstartex() Line 354 C
    msvcr110d.dll!_threadstartex(void * ptd=0x009ff308) Line 337    C
    kernel32.dll!7790495d() Unknown
    ntdll.dll!77b498ee()    Unknown
    ntdll.dll!77b498c4()    Unknown

【问题讨论】:

  • 您是否总是使用固定字符串作为文件名?确保sprintf 不超过MAX_PATH
  • 不显示伪代码,显示真实代码。
  • 顺便说一句,memset 之前的 sprintf 是不需要的,sprintf 负责用 '\0' 终止生成的字符串。
  • 缓冲区溢出会在代码中看似完全不相关的点产生影响
  • 只有我能看到sprintf(szItemAbsPath, "%s%s%s", "D:\\Root", FILE_SEPARATOR_STR, "temp.txt"); 的讽刺意味,其中(a)“抽象”分隔符也在文件名"D:\\Root" 的第一部分硬编码,以及(b ) 无论如何,整个事情都可以在初始化程序中。

标签: c winapi


【解决方案1】:

在合理的假设下,此代码不会产生此错误。这个数组的大小是短的,但它的长度仍然是 260,这对于示例来说已经足够了。

这种访问冲突强烈暗示了一个野商店。函数的一个或多个指针参数已损坏并在使用时触发访问冲突。

如果您可以通过最少的完整验证示例 (https://stackoverflow.com/help/mcve) 重现问题,那么将不乏帮助您找到错误的人。但到那时你会自己找到它的。


正如评论中所指出的,“在位置 0x000000nn”性质的访问冲突强烈表明对结构或类成员的 NULL 指针取消引用,该结构或类成员从对象的开头偏移 nn 个字节。有时它将是一个指针,其中存储了一个小整数。有时它会是一个已递增的空值。所有这些都强烈暗示数据在某处被覆盖。

【讨论】:

  • 此外,“在位置 0x000000nn”的性质的访问冲突强烈表明对结构或类成员的 NULL 指针取消引用,该结构或类成员从结构的开头偏移nn 字节。通常情况下,调试器最终会将您带到那里。在这种情况下,它看起来像一个 code 指针,它告诉我有人可能正在炸毁一个 vtable。
猜你喜欢
  • 2013-10-11
  • 2015-07-15
  • 1970-01-01
  • 1970-01-01
  • 2020-11-06
  • 2023-03-23
  • 2017-08-30
  • 2021-02-05
  • 2022-01-21
相关资源
最近更新 更多