使用 VS 2003 生成 32 位二进制文件的代码是在没有任何警告的情况下构建的。
相同的代码,无需更改任何代码,使用 Visual Studio 2010 编译器可以成功编译和链接,生成 64 位二进制文件,但带有以下警告列表。
所以,我的问题是, 下面列表中的任何警告是否是运行时需要关注的问题?
pcd.c(248) : warning C4996: 'getenv': This function or variable may be unsafe. Consider using _dupenv_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
pcd.c(377) : warning C4244: '=' : conversion from 'uintptr_t' to 'ULONG', possible loss of data
pcd.c(236) : warning C4100: 'argv' : unreferenced formal parameter
i.c(183) : warning C4100: 'lpReserved' : unreferenced formal parameter
api.c(506) : warning C4996: 'stricmp': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _stricmp. See online help for details.
api.c(554) : warning C4310: cast truncates constant value
api.c(719) : warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
api.c(2217) : warning C4996: 'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
api.c(2892) : warning C4312: 'type cast' : conversion from 'ULONG_T' to 'HANDLE_T' of greater size
api.c(559) : warning C4702: unreachable code
stdio.h(234) : see declaration of 'fopen'
api.c(2217) : warning C4996: 'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
prm.c(681) : warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\stdio.h(234) : see declaration of 'fopen'
host.c(410) : warning C4311: 'type cast' : pointer truncation from 'PVOID_T' to 'unsigned long'
stub.c(138) : warning C4295: 'eye' : array is too small to include a terminating null character
isv.c(372) : warning C4310: cast truncates constant value
chp.c(250) : warning C4244: '=' : conversion from 'SOCKET' to 'ULONG_T', possible loss of data
api.c(665) : warning C4311: 'type cast' : pointer truncation from 'HANDLE_T' to 'LONG'
api.c(1216) : warning C4057: 'function' : 'LPDWORD' differs in indirection to slightly different base types from 'LONG_T *'
hlp.c(1171) : warning C4057: 'function' : 'LPDWORD' differs in indirection to slightly different base types from 'LONG_T *'
neto.c(435) : warning C4057: 'function' : 'PLONG_T' differs in indirection to slightly different base types from 'ULONG_T *'
neto.c(595) : warning C4152: nonstandard extension, function/data pointer conversion in expression
neto.c(2115) : warning C4213: nonstandard extension used : cast on l-value
neto.c(2209) : warning C4057: 'function' : 'int *' differs in indirection to slightly different base types from 'LONG *'
td.c(760) : warning C4244: '=' : conversion from 'uintptr_t' to 'int', possible loss of data
td.c(2104) : warning C4054: 'type cast' : from function pointer 'FARPROC' to data pointer 'PVOID'
msc.c(287) : warning C4133: 'function' : incompatible types - from 'long *' to 'time_t *'
msc.c(1009) : warning C4702: unreachable code
inf.c(400) : warning C4057: 'function' : 'PLONG_T' differs in indirection to slightly different base types from 'ULONG *'
arb.c(166) : warning C4267: '=' : conversion from 'size_t' to 'LONG_T', possible loss of data
arb.c(226) : warning C4244: '=' : conversion from 'int' to 'CHAR_T', possible loss of data
sl.c(441) : warning C4054: 'type cast' : from function pointer 'int (__cdecl *)(unsigned char *,int,int,void *)' to data pointer 'void *'
pco.c(369) : warning C4057: 'function' : 'PLONG_T' differs in indirection to slightly different base types from 'ULONG_T *'
exit1.c(157) : warning C4295: 'publickey' : array is too small to include a terminating null
env.c(341) : warning C4267: 'function' : conversion from 'size_t' to 'DWORD', possible loss of data
hook.c(221) : warning C4245: 'return' : conversion from 'int' to 'SOCKET', signed/unsigned mismatch
hook.c(817) : warning C4311: 'type cast' : pointer truncation from 'unsigned char *' to 'int'
tor.c(128) : warning C4244: 'function' : conversion from 'time_t' to 'unsigned int', possible loss of data
cth.c(1012) : warning C4244: '=' : conversion from '__int64' to 'int', possible loss of data
cntrl.c(427) : warning C4996: 'strnicmp': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _strnicmp. See online help for details.
api.c(263) : warning C4057: 'function' : 'int *' differs in indirection to slightly different base types from 'LONG_T *'
api.c(706) : warning C4057: 'function' : 'int *' differs in indirection to slightly different base types from 'DWORD *'
ii.c(252) : warning C4244: '=' : conversion from 'time_t' to 'long', possible loss of data
谢谢
最佳答案
我建议您更好地阅读错误消息。它们是用简单的语言写成的。您可以通过 Google 查找您尚不理解的术语。
让我们来回顾一下,好吗?
pcd.c(248) : warning C4996: 'getenv': This function or variable may be unsafe. Consider using _dupenv_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
仅当您在从另一个线程更改变量时调用 getenv
时,我才会担心这一点。这些例程的问题在于它们依赖于全局状态,这在使用多个线程时会出现问题。新版本也更清楚地说明了谁拥有什么分配。
pcd.c(377) : warning C4244: '=' : conversion from 'uintptr_t' to 'ULONG', possible loss of data
看起来很可疑。您是否正在通过将指针强制转换为整数来执行一些奇怪的事情?在 64 位 Windows 推出前后,他们为此添加了 ULONG_PTR
。
pcd.c(236) : warning C4100: 'argv' : unreferenced formal parameter
i.c(183) : warning C4100: 'lpReserved' : unreferenced formal parameter
可以安全地被忽略或抑制。
api.c(506) : warning C4996: 'stricmp': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _stricmp. See online help for details.
不是很担心。带下划线的名称最终可能会降低您在 *nix 系统中的可移植性。
api.c(554) : warning C4310: cast truncates constant value
看起来很可疑。请提供示例。
api.c(719) : warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
api.c(2217) : warning C4996: 'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
这些是每个正派的 C 程序员都应该知道或理解的古老问题。请进行谷歌搜索或阅读有关缓冲区溢出以及为什么应尽可能避免使用这些函数的内容。请注意,虽然 _s
名称位于 C1x 标准中,但它们在 *nix 系统上可能并不常见。还有符合旧标准的 snprintf
和 strncpy
。
api.c(2892) : warning C4312: 'type cast' : conversion from 'ULONG_T' to 'HANDLE_T' of greater size
不确定HANDLE_T
是什么,但在Win32中HANDLE
是指针的大小。这很糟糕。我建议像以前一样ULONG_PTR
。
api.c(559) : warning C4702: unreachable code
正是它所说的。
stdio.h(234) : see declaration of 'fopen'
你看到了吗?
api.c(2217) : warning C4996: 'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
与上面类似。
prm.c(681) : warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\stdio.h(234) : see declaration of 'fopen'
这是 MS 编译器开始推荐的另一个 C11 功能。这个有点可疑。 fopen
可能没问题。根据需要阅读文档。
host.c(410) : warning C4311: 'type cast' : pointer truncation from 'PVOID_T' to 'unsigned long'
使用ULONG_PTR
或uintptr_t
而不是unsigned long
。
stub.c(138) : warning C4295: 'eye' : array is too small to include a terminating null character
我的猜测是你声明了一个像这样的字符串:
char foo[n] = "blah";
其中 n
是一个太小而无法容纳字符串的大小。只需删除整数即可。
api.c(1216) : warning C4057: 'function' : 'LPDWORD' differs in indirection to slightly different base types from 'LONG_T *'
DWORD
和 LONG
是不同的类型,您将指向其中一种类型的指针并将其用作另一种类型。这是非法的。 (在这种情况下,它不会给您带来问题,但您应该修复它。)
neto.c(595) : warning C4152: nonstandard extension, function/data pointer conversion in expression
嗯,不知道你是怎么做到的。也许您将函数指针分配给了void *
?有几个库需要这样做,尽管从技术上来说,按照标准这是非法的。
neto.c(2115) : warning C4213: nonstandard extension used : cast on l-value
我猜你做了这样的事情:
*(char*)foo = bar;
非法。
neto.c(2209) : warning C4057: 'function' : 'int *' differs in indirection to slightly different base types from 'LONG *'
与交替使用 PDWORD
和 LONG
的情况类似。
td.c(2104) : warning C4054: 'type cast' : from function pointer 'FARPROC' to data pointer 'PVOID'
类似于 neto.c,第 595 行。
关于c++ - C 在运行时发出警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14474813/