异常处理fscanf_s示例 [英] exception handling fscanf_s example

查看:276
本文介绍了异常处理fscanf_s示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Visual Studio 2010项目属性中,我具有带有SEH异常的Enabled C ++异常.

我故意忽略了对fscanf_s的调用以测试我的异常处理的缓冲区大小,该错误处理未能捕获错误.

我使用catch(...),还利用了

In Visual Studio 2010 project properties, I have Enabled C++ exceptions with SEH Exceptions.

I intentionally omit the buffer size in a call to fscanf_s to test my exception handling, which fails to catch the error.

I used catch(...) and also utilized an article at

//http://thunderguy.com/semicolon/2002/08/15/visual-c-exception-handling/3/


上的一篇文章
有更好的参考吗?我想防止崩溃,宁愿优雅退出.



Is there a better reference? I want to prevent crashes and would rather exit gracefully.

FILE *File_Pointer=0;
            fopen_s(&File_Pointer, CSV_File.c_str(), "r");
        if (!File_Pointer)
            ;
        else {
            //the first line should be Calc_Status
            //why is this not catching the access violation?
            //is it the fscanf_s that prevents this??
            win32_exception::install_handler();
            try {
                long const BUFFER_SIZE=512;
                while (!feof(File_Pointer)) {
                    char buffer[BUFFER_SIZE];
                    fscanf_s(File_Pointer, "%s", buffer); //uncaught access violation or stack overflow without the size
                }
            }
            catch (const access_violation &c) {
                c;
            }
            catch (const win32_exception &c) {
                c;
            }
            catch(...) {
                ;
            }
            fclose(File_Pointer);

推荐答案

fscanf& fscanf_s都是 C运行时库的一部分,因此,它们不会在失败时引发异常.

要引发异常,您需要
(a)将这些函数包装在c ++代码中,并在检测到错误时自行抛出 n 异常.
(b)仅使用c ++文件处理例程.即fstream( fstream帮助cpluspls.com [
fscanf & fscanf_s are both part of the C runtime library, and as such, they don''t raise an exception on failure.

To raise an exception, you need to either
(a) wrap these functions in c++ code, throwning the exceptions yourself when an error is detected.
(b) just use c++ file-handling routines. i.e fstream (fstream help cpluspls.com[^])


除了没有在程序中使用RAII的纯粹喜剧之外为了测试异常处理,为什么在调用fscanf_s时会导致崩溃?仅当您访问尚未提交给进程的内存页面并且您的进程拥有"您要写入的所有内容时,访问冲突才会发生,而不会发生崩溃.

当scanf系列之一覆盖缓冲区时,它将破坏/破坏该函数所调用的堆栈以及调用该函数的堆栈.因此,崩溃只会最早在出现以下示例时出现 :

-您取消引用了现在无效的指针或引用
-尝试返回没有映射代码页的地方的中间
-您引发了一个异常,该异常试图访问已删除的异常记录[1]
-返回您不应在过程代码中找到的任何地方,并且
-尝试执行一条非法指令(例如您回到了指令中间)
-由于堆栈对于您的功能而言不是有效的堆栈,因此您可以执行上述任何操作

由于您的代码在try块完成后直到 都不会做任何这些事情,因此除非编写了代码,否则引发异常的机会不大处理它已经不复存在了.

如果要测试是否发生了Win32结构化异常并被捕获,请不要与标准库中的函数打交道-做一些真正残酷而直接的事情:
Aside from the sheer comedy of not using RAII in a program to test exception handling, why do you expect a crash in the call to fscanf_s? Access violations only happen when you access pages of memory that hasn''t been committed to a process and as your process "owns" everything you''re writing to it won''t crash.

When one of the scanf family overwrites the buffer it''ll damage/destroy the stack for the function it''s called in and those that called it. So the crash will only occur at the earliest when something like these examples happen:

- you dereference a now invalid pointer or reference
- try and return into the middle of somewhere there are no code pages mapped
- you raise an exception that tries to access a trashed exception record [1]
- return somewhere in your process''s code you shouldn''t and either
-- try and execute an illegal instruction (say you''ve returned into the middle of it)
-- as the stack won''t be a valid one for your function you do any of the things above

As your code doesn''t do any of these things until after the try block has completed there''s not a lot of chance of the exception being raised until after the code that''s been written to handle it has been and gone.

If you want to test whether win32 structured exceptions are happening and being caught don''t arse about with functions from the standard library - do something really brutal and direct:
int *p = 0; *p = 0;


无论您的异常过滤器是否正常运行,这都会非常迅速地显示出来.

干杯,



[1]适用于使用基于代码的异常的编译器

编辑有效的缩进功能


That''ll show up really quickly whether your exception filter is working or not.

Cheers,

Ash

[1] for compilers that use code based exceptions

Edit for effing useless indent feature


我知道我不需要使用fscanf.问题是我通常试图防止未捕获的运行时异常,因为它们在我的应用程序中是完全不可接受的.我应该只返回错误代码,而不会使整个应用程序崩溃.
I understand that I do not need to use fscanf. The issue is that I am trying to prevent uncaught run-time exceptions in general since they are completely unacceptable in my application. I should only return an error code and not cause a whole application to crash.


这篇关于异常处理fscanf_s示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆