Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

There is a possibility for race condition in file.php #15

Open
zerkms opened this issue Oct 28, 2012 · 0 comments
Open

There is a possibility for race condition in file.php #15

zerkms opened this issue Oct 28, 2012 · 0 comments

Comments

@zerkms
Copy link

zerkms commented Oct 28, 2012

        // If file does not exist
        if ( ! $file->isFile())
        {
            // Return default value
            return $default;
        }
        else
        {
            // Open the file and extract the json
            $json = $file->openFile()->current();

            // Decode the json into PHP object
            $data = json_decode($json);

            // Test the expiry
            if ($data->expiry < time())
            {
                // Delete the file
                $this->_delete_file($file, NULL, TRUE);

                // Return default value
                return $default;
            }
            else
            {
                return ($data->type === 'string') ? $data->payload : unserialize($data->payload);
            }
        }

There is a chance to read the file contents while another thread is writing to a file.

I've caught that just once for 2 years, and this is how the exception looked like:

 Kohana_Cache_Exception [ 0 ]: Kohana_Cache_File::get failed to unserialize cached object with message : Trying to get property of non-object ~ MODPATH/cache/classes/kohana/cache/file.php [ 170 ]

So I think that the file was written only partially and $json contained malformed json, what leaded to a NULL in $data.

Proposed solution - add if (!$data) return $default; after decoding

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant