Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4012960
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:18:43+00:00 2026-05-20T09:18:43+00:00

From 19 Deadly Sins of Software Security ; The following code is the poster

  • 0

From 19 Deadly Sins of Software Security;


     The following code is the poster child for the file-access race condition defect. In between the call to access(2) and open(2), the operating system could switch away from your running process and give another process a time slice. In the interveneing time, the file /tmp/splat could be deleted, and then the application crashes.

    …
    const char *filename="/tmp/splat";
    if (access(filename, R_OK)==0) {
      int fd=open(filename, O_RDONLY);
      handle_file_contents(fd);
      close(fd);
    }

and


     Again, this code is accessing the file using a filename. The code determines if the file is readable by the effective user of the Perl script and if it is, reads it. This sinful code is similar to the C/C++ code: between the file check and the read, the file may have disappeared.

    #!/user/bin/perl
    my $file="$ENV{HOME}/.config";
    read_config($file) if -r $file;

and finally,


     Use a file handle, not the filename, to verify the file exists and then open it.

    $!/ur/bin/perl
    my $file="$ENV{HOME}/.config";
    if (open(FILE, "< $file")) {
      read_config(*FILE) if is_accessible(*FILE);
    }

The point is that if you use a filename for each call to a file-related function, the file could be changed, deleted, etc. between calls, particularly on a remote server. It’s better to use a file handle or file descriptor. Unfortunately, the PHP manual seems to indicate that most file functions only work on a string representing the filename and don’t have overloads that can take a handle instead, filesize in particular:

  $fn = "somefile.txt"
  $fh = fopen($fn);
  if ($fh !== FALSE) {
    $data = fread($fh, filesize($fn));
  }

That’s not good; between the call to fopen and filesize, the file could have been altered. Worse, the file could have been altered between the call to filesize and the meat of fread!

Does anyone know of a way to use PHP file functions, especially filesize with handles instead of filenames?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T09:18:44+00:00Added an answer on May 20, 2026 at 9:18 am

    A lot of the data you’re looking for can be accessed with fstat().

    The following can be derived from an fstat() call. See stat() for more about the information fstat() returns. From the PHP manual:

    Array
    (
        [dev] => 771
        [ino] => 488704
        [mode] => 33188
        [nlink] => 1
        [uid] => 0
        [gid] => 0
        [rdev] => 0
        [size] => 1114
        [atime] => 1061067181
        [mtime] => 1056136526
        [ctime] => 1056136526
        [blksize] => 4096
        [blocks] => 8
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

From following .xml file I set xml to show TabHost but after i run
From a desktop application developer point of view, is there any difference between developing
From time to time I see an enum like the following: [Flags] public enum
From code analysis (Visual studio), I got this warning: Warning 2 CA2000 : Microsoft.Reliability
From the html source file i've to identify tag with inline style attribute using
I am attempting to pull some information from my tnsnames file using regex. I
I'm in the process of porting some code from Linux to Mac OS X.
From the ethreal packet capture, I see the following behaviour which appears quite strange
From my xhtml file: <h:inputTextarea value=#{newPostForm.post.body}> <f:converter converterId=NL2BRConverter /> </h:inputTextarea> From my java file:
From a web developer point of view, what changes are expected in the development

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.