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

fopen r+ can lead to malformed json config file #39

Closed
ypereirareis opened this issue Nov 23, 2015 · 4 comments
Closed

fopen r+ can lead to malformed json config file #39

ypereirareis opened this issue Nov 23, 2015 · 4 comments

Comments

@ypereirareis
Copy link
Contributor

Hi !

To persist the satis config file you use this function from src/Playbloom/Satisfy/Model/FilePersister.php:

protected function dumpFile($filename, $content)
    {
        $handle = fopen($filename, 'r+');
        if (!$handle) {
            throw new IOException(sprintf('Failed to open file "%s" for write', $filename), 0, null, $filename);
        }

        $locked = flock($handle, LOCK_EX | LOCK_NB);
        if (!$locked) {
            throw new IOException(sprintf('Failed to lock file "%s"', $filename), 0, null, $filename);
        }

        ftruncate($handle);
        fwrite($handle, $content);
        fclose($handle);
    }

But I think there is a problem with the used file mode $handle = fopen($filename, 'r+'); because it can leads to malformed json, because this mode *does not * empty the file before writing.

Actually, when i try to delete a repository from my satis.json file I get this king of error:

RuntimeException in JsonDeserializationVisitor.php line 43:
Could not decode JSON, syntax error - malformed JSON.

And JSON config file

{
    "name": "My Private Repo",
    "homepage": "trololo.com",
    "output-dir": "\/app",
    "repositories": [],
    "require": [],
    "require-all": true,
    "require-dependencies": true,
    "require-dev-dependencies": true,
    "archive": {
        "skip-dev": true
    },
    "minimum-stability": "dev"
}cies":true,"archive":{"skip-dev":true},"minimum-stability":"dev"}

This is because my new content is smaller than the previous file content.

You should try using w+ option to empty the file before writing, I think !!

Thx for your work !! :)

@ypereirareis
Copy link
Contributor Author

Hi again !!

Or maybe a modification of the truncate function: ftruncate($handle,0);

Bye !

@ludofleury
Copy link
Collaborator

I think it's related with #38 ?

@ypereirareis
Copy link
Contributor Author

Yes ! Indeed !

And i tried the PR #38 yesterday, it works like a charm ! 👍

Thx

@ramunasd
Copy link
Collaborator

fixed in #38

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

3 participants