From 7f32b71b6b8f11a8324ef540fefbc129b3cfb794 Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Sun, 30 Nov 2025 13:59:29 +0100 Subject: [PATCH] libstore/derivations: expand error message for the first misparse If the derivation does not start with D, do not return a simple "expected string 'D'" error but a full error message. This contributes towards #447. Change-Id: Iee05f3918e4cc43e79f244ab2fd64a52cf2bb6d2 Signed-off-by: Raito Bezarius --- lix/libstore/derivations.cc | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/lix/libstore/derivations.cc b/lix/libstore/derivations.cc index f7b005450..22f524fb0 100644 --- a/lix/libstore/derivations.cc +++ b/lix/libstore/derivations.cc @@ -142,6 +142,13 @@ static void expect(StringViewStream & str, std::string_view s) str.remaining.remove_prefix(s.size()); } +static void expectWithErrorMsg(StringViewStream & str, std::string_view s, const char * errorMsg) +{ + if (!str.remaining.starts_with(s)) { + throw FormatError(errorMsg); + } + str.remaining.remove_prefix(s.size()); +} /* Read a C-style string from stream `str'. */ static BackedStringView parseString(StringViewStream & str) @@ -276,13 +283,16 @@ Derivation parseDerivation( drv.name = name; StringViewStream str{s}; - expect(str, "D"); + + const char * genericErrorMsg = "derivation does not start with 'Derive' or 'DrvWithVersion'"; + + expectWithErrorMsg(str, "D", genericErrorMsg); switch (str.peek()) { case 'e': - expect(str, "erive("); + expectWithErrorMsg(str, "erive(", genericErrorMsg); break; case 'r': { - expect(str, "rvWithVersion("); + expectWithErrorMsg(str, "rvWithVersion(", genericErrorMsg); auto versionS = parseString(str); throw FormatError("Unknown derivation ATerm format version '%s'", *versionS); }