Skip to content

Fix memenv RenameFile when source and target are the same - #1376

Open
Huang-404-Q wants to merge 1 commit into
google:mainfrom
Huang-404-Q:fix/memenv-rename-self
Open

Huang-404-Q wants to merge 1 commit into
google:mainfrom
Huang-404-Q:fix/memenv-rename-self

Conversation

@Huang-404-Q

Copy link
Copy Markdown

InMemoryEnv::RenameFile(src, target) removes the target before copying the source entry, so when src == target it deletes the only map entry and then re-inserts nothing. The call returns OK, but the file is gone. POSIX rename(2) with identical paths is a successful no-op that preserves the file.

Repro (added as MemEnvTest.RenameFileToItself):

env->NewWritableFile("/dir/f", &w);
w->Append("abc");
delete w;
env->RenameFile("/dir/f", "/dir/f");  // OK
env->FileExists("/dir/f");            // false before the fix, true after
env->GetFileSize("/dir/f", &size);    // IOError before, 3 after

Fix: return OK before touching the map when src == target.

Verification (MinGW, CMake + Ninja):

cmake --build build --target leveldb_tests -j 8
./build/leveldb_tests.exe --gtest_filter='MemEnvTest.RenameFileToItself'

Without the fix the test fails (FileExists is false); with it, MemEnvTest.* passes 8/8. No existing open PR touches helpers/memenv.

InMemoryEnv::RenameFile removes the target before copying the source
entry, so renaming a file to itself deletes it while returning OK. POSIX
rename(2) with identical paths is a successful no-op that preserves the
file.

Return OK before touching the map when src == target, and add a test
that checks the file still exists with its original size.
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

Successfully merging this pull request may close these issues.

1 participant