libexpr: drop bison-specific \0\0 input trailer

bison needed it for internal reasons, the new parser does not.

Change-Id: I91f7aa23fb151e3152eee793a4fdce423fcf98d9
This commit is contained in:
eldritch horrors 2024-11-27 02:09:08 +01:00
parent 37aeb3059d
commit 85d600ca4d
4 changed files with 6 additions and 27 deletions

View file

@ -2690,8 +2690,6 @@ Expr & EvalState::parseExprFromFile(const SourcePath & path)
Expr & EvalState::parseExprFromFile(const SourcePath & path, std::shared_ptr<StaticEnv> & staticEnv) Expr & EvalState::parseExprFromFile(const SourcePath & path, std::shared_ptr<StaticEnv> & staticEnv)
{ {
auto buffer = path.readFile(); auto buffer = path.readFile();
// readFile hopefully have left some extra space for terminators
buffer.append("\0\0", 2);
return *parse(buffer.data(), buffer.size(), Pos::Origin(path), path.parent(), staticEnv); return *parse(buffer.data(), buffer.size(), Pos::Origin(path), path.parent(), staticEnv);
} }
@ -2703,12 +2701,8 @@ Expr & EvalState::parseExprFromString(
const FeatureSettings & featureSettings const FeatureSettings & featureSettings
) )
{ {
// NOTE this method (and parseStdin) must take care to *fully copy* their input auto s = make_ref<std::string>(std::move(s_));
// into their respective Pos::Origin until the parser stops overwriting its input return *parse(s->data(), s->size(), Pos::String{.source = s}, basePath, staticEnv, featureSettings);
// data.
auto s = make_ref<std::string>(s_);
s_.append("\0\0", 2);
return *parse(s_.data(), s_.size(), Pos::String{.source = s}, basePath, staticEnv, featureSettings);
} }
@ -2724,15 +2718,9 @@ Expr & EvalState::parseExprFromString(
Expr & EvalState::parseStdin() Expr & EvalState::parseStdin()
{ {
// NOTE this method (and parseExprFromString) must take care to *fully copy* their
// input into their respective Pos::Origin until the parser stops overwriting its
// input data.
//Activity act(*logger, lvlTalkative, "parsing standard input"); //Activity act(*logger, lvlTalkative, "parsing standard input");
auto buffer = drainFD(0); auto s = make_ref<std::string>(drainFD(0));
// drainFD should have left some extra space for terminators return *parse(s->data(), s->size(), Pos::Stdin{.source = s}, CanonPath::fromCwd(), staticBaseEnv);
auto s = make_ref<std::string>(buffer);
buffer.append("\0\0", 2);
return *parse(buffer.data(), buffer.size(), Pos::Stdin{.source = s}, CanonPath::fromCwd(), staticBaseEnv);
} }

View file

@ -39,11 +39,6 @@ Expr * EvalState::parse(
featureSettings, featureSettings,
}; };
assert(length >= 2);
assert(text[length - 1] == 0);
assert(text[length - 2] == 0);
length -= 2;
p::string_input<p::tracking_mode::lazy> inp{std::string_view{text, length}, "input"}; p::string_input<p::tracking_mode::lazy> inp{std::string_view{text, length}, "input"};
try { try {
parser::v1::ExprState x; parser::v1::ExprState x;

View file

@ -2851,9 +2851,7 @@ void EvalState::createBaseEnv()
building baseEnv/staticBaseEnv because it uses 'builtins'. */ building baseEnv/staticBaseEnv because it uses 'builtins'. */
char code[] = char code[] =
#include "primops/derivation.nix.gen.hh" #include "primops/derivation.nix.gen.hh"
// the parser needs two NUL bytes as terminators; one of them ;
// is implied by being a C string.
"\0";
eval(*parse(code, sizeof(code), derivationInternal, {CanonPath::root}, staticBaseEnv), *vDerivation); eval(*parse(code, sizeof(code), derivationInternal, {CanonPath::root}, staticBaseEnv), *vDerivation);
} }

View file

@ -79,9 +79,7 @@ void writeFull(int fd, std::string_view s, bool allowInterrupts)
std::string drainFD(int fd, bool block, const size_t reserveSize) std::string drainFD(int fd, bool block, const size_t reserveSize)
{ {
// the parser needs two extra bytes to append terminating characters, other users will StringSink sink(reserveSize);
// not care very much about the extra memory.
StringSink sink(reserveSize + 2);
sink << drainFDSource(fd, block); sink << drainFDSource(fd, block);
return std::move(sink.s); return std::move(sink.s);
} }