From d585812cc98f488df485742cbd2197c1c5a52e17 Mon Sep 17 00:00:00 2001 From: Martin Krulis Date: Sat, 18 Aug 2018 20:37:26 +0200 Subject: [PATCH] Fixing bug in token judge: Empty files are now processed correctly. --- .../bpplib/system/mmap_file.hpp | 41 ++++++++++--------- .../tests/13-empty-files.bats | 9 ++++ .../recodex_token_judge/tests/13.correct.in | 0 judges/recodex_token_judge/tests/13.result.in | 0 recodex-worker.spec | 6 +-- 5 files changed, 34 insertions(+), 22 deletions(-) create mode 100644 judges/recodex_token_judge/tests/13-empty-files.bats create mode 100644 judges/recodex_token_judge/tests/13.correct.in create mode 100644 judges/recodex_token_judge/tests/13.result.in diff --git a/judges/recodex_token_judge/bpplib/system/mmap_file.hpp b/judges/recodex_token_judge/bpplib/system/mmap_file.hpp index 3795c64f..ff1c775f 100644 --- a/judges/recodex_token_judge/bpplib/system/mmap_file.hpp +++ b/judges/recodex_token_judge/bpplib/system/mmap_file.hpp @@ -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); @@ -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 } @@ -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 } diff --git a/judges/recodex_token_judge/tests/13-empty-files.bats b/judges/recodex_token_judge/tests/13-empty-files.bats new file mode 100644 index 00000000..0e882e22 --- /dev/null +++ b/judges/recodex_token_judge/tests/13-empty-files.bats @@ -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 ] +} diff --git a/judges/recodex_token_judge/tests/13.correct.in b/judges/recodex_token_judge/tests/13.correct.in new file mode 100644 index 00000000..e69de29b diff --git a/judges/recodex_token_judge/tests/13.result.in b/judges/recodex_token_judge/tests/13.result.in new file mode 100644 index 00000000..e69de29b diff --git a/recodex-worker.spec b/recodex-worker.spec index 1d6abd85..59cd606d 100644 --- a/recodex-worker.spec +++ b/recodex-worker.spec @@ -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