From a7f00a4e84f1db436017172dd96b8ea42abc2723 Mon Sep 17 00:00:00 2001 From: krangelov Date: Sun, 12 Sep 2021 08:11:10 +0200 Subject: [PATCH] detect and report an attempt to load non .ngf file in readNGF --- src/runtime/c/pgf/db.cxx | 13 +++++++++++++ src/runtime/haskell/tests/basic.hs | 14 ++++++-------- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/runtime/c/pgf/db.cxx b/src/runtime/c/pgf/db.cxx index 9c7f3ca7a..d96888e00 100644 --- a/src/runtime/c/pgf/db.cxx +++ b/src/runtime/c/pgf/db.cxx @@ -250,8 +250,16 @@ typedef struct mchunk mbin; */ #define FASTBIN_CONSOLIDATION_THRESHOLD (65536UL) +static char slovo[5] = {'S','L','O','V','O'}; + struct PGF_INTERNAL_DECL malloc_state { + /* Each .ngf file starts with 'SLOVO' as in: + * "V načaloto be slovoto" (In the beginning was the word) + * In this way we detect an attempt to read a non .ngf file. + */ + char sign[5]; + /* Set if the fastbin chunks contain recently inserted free blocks. */ bool have_fastchunks; /* Fastbins */ @@ -320,6 +328,9 @@ PgfDB::PgfDB(const char* filepath, int flags, int mode) { if (is_new) { init_state(file_size); } else { + if (strncmp(ms->sign, slovo, sizeof(ms->sign)) != 0) + throw pgf_error("Invalid file content"); + // We must make sure that left-over transient revisions are // released. This may happen if a client process was killed // or if the garbadge collector has not managed to run @@ -366,6 +377,8 @@ void PgfDB::set_revision(ref pgf) PGF_INTERNAL void PgfDB::init_state(size_t size) { + memcpy(ms->sign, slovo, sizeof(ms->sign)); + /* Init fastbins */ ms->have_fastchunks = false; for (int i = 0; i < NFASTBINS; ++i) { diff --git a/src/runtime/haskell/tests/basic.hs b/src/runtime/haskell/tests/basic.hs index 0a5783e67..a2a28aaba 100644 --- a/src/runtime/haskell/tests/basic.hs +++ b/src/runtime/haskell/tests/basic.hs @@ -79,29 +79,27 @@ main = do rp1 <- testLoadFailure (readPGF "non-existing.pgf") rp2 <- testLoadFailure (readPGF "tests/basic.gf") - -- rp3 <- testLoadFailure (readPGF "tests/basic.ngf") + rp3 <- testLoadFailure (readPGF "tests/basic.ngf") bn1 <- testLoadFailure (bootNGF "non-existing.pgf" "non-existing.ngf") bn2 <- testLoadFailure (bootNGF "tests/basic.gf" "tests/basic.ngf") bn3 <- testLoadFailure (bootNGF "tests/basic.ngf" "tests/basic.pgf") - -- rn1 <- testLoadFailure (readNGF "non-existing.ngf") - -- rn2 <- testLoadFailure (readNGF "tests/basic.gf") - -- rn3 <- testLoadFailure (readNGF "tests/basic.pgf") + rn2 <- testLoadFailure (readNGF "tests/basic.gf") + rn3 <- testLoadFailure (readNGF "tests/basic.pgf") runTestTTAndExit $ TestList $ [TestCase (assertBool "missing file" rp1) ,TestCase (assertBool "wrong file format (GF)" rp2) - -- ,TestCase (assertBool "wrong file format (NGF)" rp3) + ,TestCase (assertBool "wrong file format (NGF)" rp3) ,TestCase (assertBool "missing file" bn1) ,TestCase (assertBool "wrong file format (GF)" bn2) ,TestCase (assertBool "wrong file format (NGF)" bn3) - -- ,TestCase (assertBool "missing file" rn1) - -- ,TestCase (assertBool "wrong file format (GF)" rn2) - -- ,TestCase (assertBool "wrong file format (PGF)" rn3) + ,TestCase (assertBool "wrong file format (GF)" rn2) + ,TestCase (assertBool "wrong file format (PGF)" rn3) ] ++ grammarTests gr1 ++ grammarTests gr2