1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-29 19:03:16 +02:00

Don't interpret strings as format strings

Ludo reported this error:

  unexpected Nix daemon error: boost::too_few_args: format-string refered to more arguments than were passed

coming from this line:

  printMsg(lvlError, run.program + ": " + string(err, 0, p));

The problem here is that the string ends up implicitly converted to a
Boost format() object, so % characters are treated specially.  I
always assumed (wrongly) that strings are converted to a format object
that outputs the string as-is.

Since this assumption appears in several places that may be hard to
grep for, I've added some C++ type hackery to ensures that the right
thing happens.  So you don't have to worry about % in statements like

  printMsg(lvlError, "foo: " + s);

or

  throw Error("foo: " + s);
This commit is contained in:
Eelco Dolstra 2014-03-28 16:59:26 +01:00
parent 24cb65efc3
commit 49009573bc
3 changed files with 32 additions and 23 deletions

View file

@ -125,11 +125,11 @@ private:
public:
Nest();
~Nest();
void open(Verbosity level, const format & f);
void open(Verbosity level, const FormatOrString & fs);
void close();
};
void printMsg_(Verbosity level, const format & f);
void printMsg_(Verbosity level, const FormatOrString & fs);
#define startNest(varName, level, f) \
Nest varName; \
@ -146,7 +146,7 @@ void printMsg_(Verbosity level, const format & f);
#define debug(f) printMsg(lvlDebug, f)
void warnOnce(bool & haveWarned, const format & f);
void warnOnce(bool & haveWarned, const FormatOrString & fs);
void writeToStderr(const string & s);