An exception thrown by improper use of fclose

Source: Internet
Author: User

A service that has a background transfer file on the server is frequently reported as an exception and can only be forcibly terminated and restarted.

Yesterday was just free, the scene grabbed a dump, and then threw the program into IDA to see a bit, quickly find out why, the original call fclose error.

Using the C runtime function for file operations, which is fopen,fread,ftell,fclose, is not a problem in itself.

But the location of the exception to the previous point, the program wrote a log, the main content is "Open file failed, error reason: xxxxxx"

That is, after the file has failed to open with fopen, the program still uses fclose to close the invalid file pointer, causing the exception to occur.

You can simply test it with the following code:

voidTestfun () {FILE*FP; FP= fopen ("C:\\nosuchfile.sys","R");//open a file that does not existprintf"fp = 0x%x\n", FP); if(fp = =NULL) {printf ("Open file failed.\n"); }    Else{printf ("Open file succeed.\n"); } fclose (FP);//this throws an exception}

In fact, as I know, closing an invalid handle with CloseHandle also throws an exception.

Originally opened operation is a failure, the file pointer is not valid, and then what is the significance of it?

Solution: Directly to the program in the call to a patch, with NOP instructions to fill a bit, it is normal. As shown, the color three instructions are populated with NOP:

An exception thrown by improper use of fclose

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.