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

Saving a file by overwriting its content is unsafe #13491

Open
core-ai-bot opened this issue Aug 31, 2021 · 1 comment
Open

Saving a file by overwriting its content is unsafe #13491

core-ai-bot opened this issue Aug 31, 2021 · 1 comment

Comments

@core-ai-bot
Copy link
Member

Issue by jiangyy
Wednesday Jan 20, 2016 at 05:25 GMT
Originally opened as adobe/brackets#12103


We have traced down that when saving a file, the library finally invokes a sequence like the following (we believe that is called by brackets/src/file/FileUtils.js:110):

(1) fd = open(somefile, O_TRUNC)
(2) write(fd, something)
(3) ...
(4) close(fd)

When the system crashes in between, there may be only partial data persisted in the file system (remaining is lost). The recommended way is to save a file is first write to a temporary file, and then rename it to replace the original file, to ensure that every user-data is manipulated atomically.

This problem is discovered by an automated tool, and this generally affects almost any Linux distribution. I did not find the exact reference material for Node, but here are some arguments for POSIXly correct ways to overwrite a file: http://stackoverflow.com/questions/1812115/how-to-safely-write-to-a-file.

@core-ai-bot
Copy link
Member Author

Comment by petetnt
Wednesday Jan 20, 2016 at 07:06 GMT


I was thinking about this when debugging adobe/brackets#11826 yesterday. It wasn't the cause for that particular issue, but what@jiangyy states good to me.

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