unsigned long的代码的一个漏洞

sdchenchen2012 2013-11-28 03:10:29
DWORD dwPutSize = 0; //windows下的代码


dwPutSize = ....;

if (dwPutSize > m_file.GetLength() - dwFinishSize)
dwPutSize = m_file.GetLength() - dwFinishSize; // 这里会溢出吗?

如果geglength<dwFinishSize,那么相减后是一个很大的非负数。

dwPutSize的值自然不是 我们所需要的。




...全文
301 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
lm_whales 2013-12-20
  • 打赏
  • 举报
回复
那就这么处理吧!保险点。 if(m_file.GetLength() >= dwFinishSize){ if (dwPutSize > m_file.GetLength() - dwFinishSize) dwPutSize = m_file.GetLength() - dwFinishSize; ..... }
ForestDB 2013-12-20
  • 打赏
  • 举报
回复
深入理解了数据存储的原理就好了。
sdchenchen2012 2013-12-20
  • 打赏
  • 举报
回复
引用 12 楼 ForestDB 的回复:
需要自己先行检查。 C会把signed cast成unsigned进行运算。
c语言真烦人, 按照这种说法, 可以说,进行运算的话,非常麻烦,非常受罪,非常累。 比如,windows下很多api都是dword 也就是非符号数了。 一旦有int参与,会很痛苦的, 一旦不小心,结果就喝你想的不一样。
赵4老师 2013-12-20
  • 打赏
  • 举报
回复
__int64 _filelengthi64( int handle );
赵4老师 2013-12-20
  • 打赏
  • 举报
回复
GetFileSize The GetFileSize function retrieves the size, in bytes, of the specified file. DWORD GetFileSize( HANDLE hFile, // handle of file to get size of LPDWORD lpFileSizeHigh // pointer to high-order word for file size ); Parameters hFile Specifies an open handle of the file whose size is being returned. The handle must have been created with either GENERIC_READ or GENERIC_WRITE access to the file. lpFileSizeHigh Pointer to the variable where the high-order word of the file size is returned. This parameter can be NULL if the application does not require the high-order word. Return Values If the function succeeds, the return value is the low-order doubleword of the file size, and, if lpFileSizeHigh is non-NULL, the function puts the high-order doubleword of the file size into the variable pointed to by that parameter. If the function fails and lpFileSizeHigh is NULL, the return value is 0xFFFFFFFF. To get extended error information, call GetLastError. If the function fails and lpFileSizeHigh is non-NULL, the return value is 0xFFFFFFFF and GetLastError will return a value other than NO_ERROR. Remarks You cannot use the GetFileSize function with a handle of a nonseeking device such as a pipe or a communications device. To determine the file type for hFile, use the GetFileType function. The GetFileSize function obtains the uncompressed size of a file. Use the GetCompressedFileSize function to obtain the compressed size of a file. Note that if the return value is 0xFFFFFFFF and lpFileSizeHigh is non-NULL, an application must call GetLastError to determine whether the function has succeeded or failed. The following sample code illustrates this point: // // Case One: calling the function with // lpFileSizeHigh == NULL // Try to obtain hFile's size dwSize = GetFileSize (hFile, NULL) ; // If we failed ... if (dwSize == 0xFFFFFFFF) { // Obtain the error code. dwError = GetLastError() ; // Deal with that failure. . . . } // End of error handler // // Case Two: calling the function with // lpFileSizeHigh != NULL // Try to obtain hFile's huge size. dwSizeLow = GetFileSize (hFile, & dwSizeHigh) ; // If we failed ... if (dwSizeLow == 0xFFFFFFFF && (dwError = GetLastError()) != NO_ERROR ){ // Deal with that failure. . . . } // End of error handler. QuickInfo Windows NT: Requires version 3.1 or later. Windows: Requires Windows 95 or later. Windows CE: Requires version 1.0 or later. Header: Declared in winbase.h. Import Library: Use kernel32.lib. See Also File I/O Overview, File Functions, GetCompressedFileSize, GetFileType
mujiok2003 2013-12-19
  • 打赏
  • 举报
回复
引用 楼主 sdchenchen2012 的回复:
如果geglength<dwFinishSize,那么相减后是一个很大的非负数。
试试:
unsigned long x = 0,  y = ~0; 	
	unsigned long  z = x - y;
ForestDB 2013-12-19
  • 打赏
  • 举报
回复
需要自己先行检查。 C会把signed cast成unsigned进行运算。
sdchenchen2012 2013-12-19
  • 打赏
  • 举报
回复
最后一个问题: 如果无符号数和有符号数字 参与运算,c/c++如何处理的, 比如把所有的转换为unsigned 还是signed,或者其他? 大神们解答一下,多谢
sdchenchen2012 2013-12-19
  • 打赏
  • 举报
回复
引用 9 楼 sdchenchen2012 的回复:
[quote=引用 7 楼 adeng1919 的回复:] [quote=引用 6 楼 sdchenchen2012 的回复:] [quote=引用 5 楼 adeng1919 的回复:] dwFinishSize值是怎么取的?看名字应该小于m_file.GetLength() 才对
万一打了,怎么办? [/quote]什么叫万一?你前面代码dwFinishSize怎么处理的不看看?[/quote] 怎么不可能, getlength明显是一个函数, 文件长度的函数,谁知道会不会大,或者小,你说呢?
引用 8 楼 ForestDB 的回复:
还知道整数运算的上溢和下溢?
我觉得严谨的话, 应该是: [/quote] if (dwPutSize > m_file.GetLength() - dwFinishSize) dwPutSize = m_file.GetLength() - dwFinishSize; // 这里会溢出吗? 如果geglength<dwFinishSize,那么相减后是一个很大的非负数。 严谨的话,改成: if( m_file.GetLength() < dwFinishSize ) { } else { DWORD dwPutSize = 0; //windows下的代码 dwPutSize = ....; if (dwPutSize > m_file.GetLength() - dwFinishSize) dwPutSize = m_file.GetLength() - dwFinishSize; }
sdchenchen2012 2013-12-19
  • 打赏
  • 举报
回复
引用 7 楼 adeng1919 的回复:
[quote=引用 6 楼 sdchenchen2012 的回复:] [quote=引用 5 楼 adeng1919 的回复:] dwFinishSize值是怎么取的?看名字应该小于m_file.GetLength() 才对
万一打了,怎么办? [/quote]什么叫万一?你前面代码dwFinishSize怎么处理的不看看?[/quote] 怎么不可能, getlength明显是一个函数, 文件长度的函数,谁知道会不会大,或者小,你说呢?
引用 8 楼 ForestDB 的回复:
还知道整数运算的上溢和下溢?
我觉得严谨的话, 应该是:
ForestDB 2013-11-28
  • 打赏
  • 举报
回复
还知道整数运算的上溢和下溢?
懒懒的吉他手 2013-11-28
  • 打赏
  • 举报
回复
引用 6 楼 sdchenchen2012 的回复:
[quote=引用 5 楼 adeng1919 的回复:] dwFinishSize值是怎么取的?看名字应该小于m_file.GetLength() 才对
万一打了,怎么办? [/quote]什么叫万一?你前面代码dwFinishSize怎么处理的不看看?
sdchenchen2012 2013-11-28
  • 打赏
  • 举报
回复
引用 5 楼 adeng1919 的回复:
dwFinishSize值是怎么取的?看名字应该小于m_file.GetLength() 才对
万一打了,怎么办?
懒懒的吉他手 2013-11-28
  • 打赏
  • 举报
回复
dwFinishSize值是怎么取的?看名字应该小于m_file.GetLength() 才对
toofunny 2013-11-28
  • 打赏
  • 举报
回复
这也叫漏洞?
sanae 2013-11-28
  • 打赏
  • 举报
回复
if(m_file.GetLength() < dwFinishSize){ ... } 不过如果API设计为dwFinishSize不可能大于等于前面那个的话可以省略
sdchenchen2012 2013-11-28
  • 打赏
  • 举报
回复
引用 1 楼 pochioly 的回复:
。。。那之前的大于判断就不大可能成立了 如果不放心,减法之前也判断一下
如何判断? m_file.GetLength() - dwFinishSize 这2个都是非负数
sanae 2013-11-28
  • 打赏
  • 举报
回复
。。。那之前的大于判断就不大可能成立了 如果不放心,减法之前也判断一下

64,681

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧