@@ 50,13 50,33 @@ struct pipe_state {
char buf[1 * 1024 * 1024];
};
+static void *xmalloc(size_t size)
+{
+ void *p = malloc(size);
+ if (p == NULL) {
+ fprintf(stderr, "out of memory\n");
+ exit(1);
+ }
+ return p;
+}
+
+static char *xstrdup(const char *s)
+{
+ char *t = strdup(s);
+ if (t == NULL) {
+ fprintf(stderr, "out of memory\n");
+ exit(1);
+ }
+ return t;
+}
+
/* concepts stolen from
* https://stackoverflow.com/questions/6137684/iterate-through-lua-table
*/
static const char **arr_from_lua_str_table(lua_State *L, int index, int *len_out)
{
int n = 0;
- const char **arr = malloc(1 * sizeof (char *));
+ const char **arr = xmalloc(1 * sizeof (char *));
const char **p;
if (arr == NULL) {
@@ 400,6 420,7 @@ static int tyarn_parse_args(lua_State *L)
size_t envoptsn = 0;
bool show_stdout = false;
bool show_stderr = false;
+ int bufn = 0;
luaL_checktype(L, 1, LUA_TTABLE);
@@ 410,17 431,11 @@ static int tyarn_parse_args(lua_State *L)
return 2;
}
- buf = malloc(args_len * sizeof (char *));
- if (buf == NULL) {
- fprintf(stderr, "out of memory\n");
- exit(1);
- }
-
- int bufn = 0;
- buf[bufn++] = strdup("tyarn");
+ buf = xmalloc(args_len * sizeof (char *));
+ buf[bufn++] = xstrdup("tyarn");
for (int i = 0; i < args_len - 2; i++) {
- buf[bufn++] = strdup(args[i]);
+ buf[bufn++] = xstrdup(args[i]);
}
while ((c = getopt(bufn, buf, ":12l:s:dECht:e:v")) != -1) {
@@ 556,7 571,7 @@ static char *str_from_offs(const char *orig, regoff_t start, regoff_t end)
p = buf + start;
buf[end] = '\0';
- return strdup(p);
+ return xstrdup(p);
}
static int tyarn_rematch(lua_State *L)