[PATCH] D56806: [llvm-objcopy] Fix crash when writing empty binary output

Jordan Rupprecht via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 24 21:48:38 PST 2019


rupprecht marked 2 inline comments as done.
rupprecht added inline comments.


================
Comment at: llvm/tools/llvm-objcopy/Buffer.cpp:25-26
+  // disk.
+  if (sys::fs::exists(FileName))
+    if (std::error_code EC = sys::fs::remove(FileName))
+      return createFileError(FileName, errorCodeToError(EC));
----------------
jakehehrlich wrote:
> Remove has a default parameter that sets weather or not it errors out if the file already exists. This removes a (well for this it doesn't actually matter in any case I've seen) race condition. To eliminate the condition entirely you need to create a temporary empty file on the same partition and then rename it into place. That behaves atomically. That's what FileBuffer is doing under the hood.
Thanks for that pointer! Even though this race condition is probably overkill to worry about, it ends up being much less code to use a temp file anyway, so I'm happy to steal it :D


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D56806/new/

https://reviews.llvm.org/D56806





More information about the llvm-commits mailing list