about summary refs log tree commit diff
path: root/src/libutil/util.hh
diff options
context:
space:
mode:
authorEelco Dolstra <eelco.dolstra@logicblox.com>2014-03-28T15·59+0100
committerEelco Dolstra <eelco.dolstra@logicblox.com>2014-03-28T15·59+0100
commit49009573bc2eacd823d57433daf1f59dfe415065 (patch)
tree7a8a15d1bc549e674f40cffdf0613de7af512b93 /src/libutil/util.hh
parent24cb65efc3c34e24fc653779a4d42cf4f31c6737 (diff)
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);
Diffstat (limited to 'src/libutil/util.hh')
-rw-r--r--src/libutil/util.hh6
1 files changed, 3 insertions, 3 deletions
diff --git a/src/libutil/util.hh b/src/libutil/util.hh
index 5d0408f9b528..8bedfea9a0de 100644
--- a/src/libutil/util.hh
+++ b/src/libutil/util.hh
@@ -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);