现在有个COM接口, 用来关闭文件的句柄,参数就是文件的句柄值, 内部是调用CloseHandle和FindClose,但任意输入一个句柄值,可能会有安全问题.
请问如何在判断输入的句柄是否有效,再去CloseHandle或FindClose?

解决方案 »

  1.   

    传递一个非法HANDLE给CloseHandle
    在Release版本,返回失败
    在Debug版本下,抛出异常
      

  2.   

    一般要有良好的习惯,handle初始化的时候置空关闭之后也同时置空关闭之前断言一下
    如果是非法的话这个close不会影响的,release只是会返回失败而已
      

  3.   

    If the application is running under a debugger, the function will throw an exception if it receives either a handle value that is not valid or a pseudo-handle value. This can happen if you close a handle twice, or if you call CloseHandle on a handle returned by the FindFirstFile function instead of calling the FindClose function.
      

  4.   

    谢谢大家.
    我用一个COM安全工具测了一下,输入的参数是2147483647, 0, -2147483647时会crash. 这应该要怎么保护?
      

  5.   

    简单的做法就是判断是否为NULL或者INVALID_HANDLE_VALUE(-1)即可。
      

  6.   

    问题现在不是在INVALID_HANDLE_VALUE时出错
      

  7.   

    http://www.phpchinaz.cn/c/VC_Basic/50_5001_1229076978.html
      

  8.   

    试试用try catch或者SEH去保护调用CloseHandle的调用。
      

  9.   

    if( HANDLE==NULL)无效;
    else 有效;