Skip to content

Commit

Permalink
Fixing bug in token judge: Empty files are now processed correctly.
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Krulis committed Aug 18, 2018
1 parent 8e2dccb commit d585812
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 22 deletions.
41 changes: 22 additions & 19 deletions judges/recodex_token_judge/bpplib/system/mmap_file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,15 @@ namespace bpp
if (!GetFileSizeEx(mFile, &tmpSize)) throw RuntimeError("Cannot get file size.");
mLength = tmpSize.QuadPart;

// Create read only mapping object.
mMappedFile = CreateFileMapping(mFile, NULL, PAGE_READONLY, 0, 0, NULL);
if (mMappedFile == NULL) throw RuntimeError("Cannot create mapped file object.");

// Map the entire file to virtual memory space.
mData = MapViewOfFile(mMappedFile, FILE_MAP_READ, 0, 0, 0);
if (mData == NULL) throw RuntimeError("Cannot map view of file.");
if (mLength > 0) {
// Create read only mapping object.
mMappedFile = CreateFileMapping(mFile, NULL, PAGE_READONLY, 0, 0, NULL);
if (mMappedFile == NULL) throw RuntimeError("Cannot create mapped file object.");

// Map the entire file to virtual memory space.
mData = MapViewOfFile(mMappedFile, FILE_MAP_READ, 0, 0, 0);
if (mData == NULL) throw RuntimeError("Cannot map view of file.");
}
#else
// Create file handle.
mFile = ::open(mFileName.c_str(), O_RDONLY);
Expand All @@ -116,11 +118,13 @@ namespace bpp
if (::fstat(mFile, &fileStat) == -1) throw RuntimeError("Cannot get file size.");
mLength = fileStat.st_size;

// Map the entire file to virtual memory space.
mData = ::mmap(NULL, mLength, PROT_READ, MAP_PRIVATE, mFile, 0);
if (mData == MAP_FAILED) {
mData = NULL;
throw RuntimeError("Cannot mmap the file.");
if (mLength > 0) {
// Map the entire file to virtual memory space.
mData = ::mmap(NULL, mLength, PROT_READ, MAP_PRIVATE, mFile, 0);
if (mData == MAP_FAILED) {
mData = NULL;
throw RuntimeError("Cannot mmap the file.");
}
}
#endif
}
Expand Down Expand Up @@ -163,16 +167,15 @@ namespace bpp
if (!opened()) return;

#ifdef _WIN32
if (!UnmapViewOfFile(mData)) throw RuntimeError("Cannot unmap view of file.");
mData = NULL;

if (!CloseHandle(mMappedFile) || !CloseHandle(mFile)) throw RuntimeError("Cannot close mapped file.");
mMappedFile = mFile = NULL;
if (mData != NULL && !UnmapViewOfFile(mData)) throw RuntimeError("Cannot unmap view of file.");
if (mMappedFile != NULL && !CloseHandle(mMappedFile)) throw RuntimeError("Cannot close mapped file.");
if (mFile != NULL && !CloseHandle(mFile)) throw RuntimeError("Cannot close mapped file.");
mData = mMappedFile = mFile = NULL;
#else
if (::munmap(mData, mLength) == -1) throw RuntimeError("Cannot unmap file.");
if (mData != NULL && ::munmap(mData, mLength) == -1) throw RuntimeError("Cannot unmap file.");
mData = NULL;

if (::close(mFile) == -1) throw RuntimeError("Cannot close mapped file.");
if (mfile != 0 && ::close(mFile) == -1) throw RuntimeError("Cannot close mapped file.");
mFile = 0;
#endif
}
Expand Down
9 changes: 9 additions & 0 deletions judges/recodex_token_judge/tests/13-empty-files.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bats

load bats-shared

@test "empty files" {
run $EXE_FILE $CORRECT_FILE $RESULT_FILE
[ "$status" -eq 0 ]
[ "${lines[0]}" -eq 1 ]
}
Empty file.
Empty file.
6 changes: 3 additions & 3 deletions recodex-worker.spec
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
%define name recodex-worker
%define short_name worker
%define version 1.5.0
%define unmangled_version 3569305c6587ba4c5082b80324cdd0ffacc1d653
%define release 2
%define version 1.5.2
%define unmangled_version 8e2dccb18b0ccfd58f09eca876ca7acf8fb6f77d
%define release 1

%define spdlog_name spdlog
%define spdlog_version 0.13.0
Expand Down

0 comments on commit d585812

Please sign in to comment.