aboutsummaryrefslogtreecommitdiff
path: root/src/libexpr
diff options
context:
space:
mode:
authorEelco Dolstra <eelco.dolstra@logicblox.com>2015-03-19 14:11:35 +0100
committerEelco Dolstra <eelco.dolstra@logicblox.com>2015-03-19 14:11:35 +0100
commitda6b704b197f19c386f0e53f5553252a226650bb (patch)
tree1c2ae6b81991b616fd269a1ace3d54f534f6c572 /src/libexpr
parentfa472794400745d896607f7ea33cd2a0adff4c71 (diff)
Check return values from malloc/strdup
Diffstat (limited to 'src/libexpr')
-rw-r--r--src/libexpr/eval.cc45
1 files changed, 34 insertions, 11 deletions
diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc
index 01f0be52d..4ae3005bb 100644
--- a/src/libexpr/eval.cc
+++ b/src/libexpr/eval.cc
@@ -21,9 +21,6 @@
#else
-#define GC_STRDUP strdup
-#define GC_MALLOC malloc
-
#define NEW new
#endif
@@ -32,6 +29,32 @@
namespace nix {
+static char * dupString(const char * s)
+{
+ char * t;
+#if HAVE_BOEHMGC
+ t = GC_strdup(s);
+#else
+ t = strdup(s);
+#endif
+ if (!t) throw std::bad_alloc();
+ return t;
+}
+
+
+static void * allocBytes(size_t n)
+{
+ void * p;
+#if HAVE_BOEHMGC
+ p = GC_malloc(n);
+#else
+ p = malloc(n);
+#endif
+ if (!p) throw std::bad_alloc();
+ return p;
+}
+
+
void Bindings::sort()
{
std::sort(begin(), end());
@@ -376,7 +399,7 @@ LocalNoInline(void addErrorPrefix(Error & e, const char * s, const string & s2,
void mkString(Value & v, const char * s)
{
- mkStringNoCopy(v, GC_STRDUP(s));
+ mkStringNoCopy(v, dupString(s));
}
@@ -386,9 +409,9 @@ void mkString(Value & v, const string & s, const PathSet & context)
if (!context.empty()) {
unsigned int n = 0;
v.string.context = (const char * *)
- GC_MALLOC((context.size() + 1) * sizeof(char *));
+ allocBytes((context.size() + 1) * sizeof(char *));
foreach (PathSet::const_iterator, i, context)
- v.string.context[n++] = GC_STRDUP(i->c_str());
+ v.string.context[n++] = dupString(i->c_str());
v.string.context[n] = 0;
}
}
@@ -396,7 +419,7 @@ void mkString(Value & v, const string & s, const PathSet & context)
void mkPath(Value & v, const char * s)
{
- mkPathNoCopy(v, GC_STRDUP(s));
+ mkPathNoCopy(v, dupString(s));
}
@@ -429,7 +452,7 @@ inline Value * EvalState::lookupVar(Env * env, const ExprVar & var, bool noEval)
Value * EvalState::allocValue()
{
nrValues++;
- return (Value *) GC_MALLOC(sizeof(Value));
+ return (Value *) allocBytes(sizeof(Value));
}
@@ -439,7 +462,7 @@ Env & EvalState::allocEnv(unsigned int size)
nrEnvs++;
nrValuesInEnvs += size;
- Env * env = (Env *) GC_MALLOC(sizeof(Env) + size * sizeof(Value *));
+ Env * env = (Env *) allocBytes(sizeof(Env) + size * sizeof(Value *));
env->size = size;
/* Clear the values because maybeThunk() and lookupVar fromWith expect this. */
@@ -460,7 +483,7 @@ Value * EvalState::allocAttr(Value & vAttrs, const Symbol & name)
Bindings * EvalState::allocBindings(Bindings::size_t capacity)
{
- return new (GC_MALLOC(sizeof(Bindings) + sizeof(Attr) * capacity)) Bindings(capacity);
+ return new (allocBytes(sizeof(Bindings) + sizeof(Attr) * capacity)) Bindings(capacity);
}
@@ -469,7 +492,7 @@ void EvalState::mkList(Value & v, unsigned int length)
clearValue(v);
v.type = tList;
v.list.length = length;
- v.list.elems = length ? (Value * *) GC_MALLOC(length * sizeof(Value *)) : 0;
+ v.list.elems = length ? (Value * *) allocBytes(length * sizeof(Value *)) : 0;
nrListElems += length;
}