summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2021-04-01 19:24:49 +0200
committerJo-Philipp Wich <jo@mein.io>2021-04-24 23:44:04 +0200
commitf2c4b79feaffd7b2fdb4041f47c9cd0f4cc3bc6e (patch)
tree4b07c881aacb2a81f027ce59fe0e8b2bb63b2eaa
parent93ededbe93ff1e5949387996b69892c9220a94e8 (diff)
treewide: fix issues reported by clang code analyzer
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
-rw-r--r--compiler.c24
-rw-r--r--lexer.c2
-rw-r--r--lib.c53
-rw-r--r--lib/fs.c1
-rw-r--r--main.c2
5 files changed, 51 insertions, 31 deletions
diff --git a/compiler.c b/compiler.c
index 7c0a2a1..757ff0c 100644
--- a/compiler.c
+++ b/compiler.c
@@ -1757,7 +1757,6 @@ uc_compiler_compile_object(uc_compiler *compiler, bool assignable)
if (len > 0) {
uc_compiler_emit_insn(compiler, compiler->parser->prev.pos, I_SOBJ);
uc_compiler_emit_u32(compiler, 0, len);
- len = 0;
}
/* set initial size hint */
@@ -2166,14 +2165,14 @@ uc_compiler_compile_for_count(uc_compiler *compiler, bool local, uc_token *var)
/* Initializer ---------------------------------------------------------- */
- /* We parsed a `local x` or `local x, y` expression, so (re)declare
- * last label as local initializer variable */
- if (local)
- uc_compiler_declare_local_null(compiler, var->pos, var->val);
-
- /* If we parsed at least on label, try continue parsing as variable
+ /* If we parsed at least one label, try continue parsing as variable
* expression... */
if (var) {
+ /* We parsed a `local x` or `local x, y` expression, so (re)declare
+ * last label as local initializer variable */
+ if (local)
+ uc_compiler_declare_local_null(compiler, var->pos, var->val);
+
uc_compiler_compile_labelexpr(compiler, true);
uc_compiler_emit_insn(compiler, 0, I_POP);
@@ -2268,10 +2267,6 @@ uc_compiler_compile_for(uc_compiler *compiler)
local = uc_compiler_parse_match(compiler, TK_LOCAL);
- if (local && !uc_compiler_parse_check(compiler, TK_LABEL))
- uc_compiler_syntax_error(compiler, compiler->parser->curr.pos,
- "Expecting label after 'local'");
-
if (uc_compiler_parse_match(compiler, TK_LABEL)) {
keyvar = compiler->parser->prev;
uc_value_get(keyvar.val);
@@ -2291,6 +2286,12 @@ uc_compiler_compile_for(uc_compiler *compiler)
goto out;
}
}
+ else if (local) {
+ uc_compiler_syntax_error(compiler, compiler->parser->curr.pos,
+ "Expecting label after 'local'");
+
+ goto out;
+ }
/*
* The previous expression ruled out a for-in loop, so continue parsing
@@ -2335,6 +2336,7 @@ uc_compiler_compile_switch(uc_compiler *compiler)
/* handle `default:` */
if (uc_compiler_parse_match(compiler, TK_DEFAULT)) {
if (default_off) {
+ uc_vector_clear(&cases);
uc_compiler_syntax_error(compiler, compiler->parser->prev.pos,
"more than one switch default case");
diff --git a/lexer.c b/lexer.c
index ada8671..1fba88f 100644
--- a/lexer.c
+++ b/lexer.c
@@ -728,7 +728,7 @@ parse_label(uc_lexer *lex, bool no_regexp)
if (!buf_remaining(lex) || (lex->bufstart[0] != '_' && !isalnum(lex->bufstart[0]))) {
for (i = 0, word = &reserved_words[0]; i < ARRAY_SIZE(reserved_words); i++, word = &reserved_words[i]) {
- if (lex->lookbehindlen == word->plen && !strncmp(lex->lookbehind, word->pat, word->plen)) {
+ if (lex->lookbehind && lex->lookbehindlen == word->plen && !strncmp(lex->lookbehind, word->pat, word->plen)) {
lookbehind_reset(lex);
switch (word->type) {
diff --git a/lib.c b/lib.c
index a4cfdea..ea87a6b 100644
--- a/lib.c
+++ b/lib.c
@@ -508,6 +508,7 @@ uc_unshift(uc_vm *vm, size_t nargs)
static json_object *
uc_chr(uc_vm *vm, size_t nargs)
{
+ json_object *rv = NULL;
size_t idx;
int64_t n;
char *str;
@@ -528,7 +529,10 @@ uc_chr(uc_vm *vm, size_t nargs)
str[idx] = (char)n;
}
- return xjs_new_string_len(str, nargs);
+ rv = xjs_new_string_len(str, nargs);
+ free(str);
+
+ return rv;
}
static json_object *
@@ -1194,6 +1198,7 @@ uc_uc(uc_vm *vm, size_t nargs)
static json_object *
uc_uchr(uc_vm *vm, size_t nargs)
{
+ json_object *rv = NULL;
size_t idx, ulen;
char *p, *str;
int64_t n;
@@ -1226,7 +1231,10 @@ uc_uchr(uc_vm *vm, size_t nargs)
break;
}
- return xjs_new_string_len(str, ulen);
+ rv = xjs_new_string_len(str, ulen);
+ free(str);
+
+ return rv;
}
static json_object *
@@ -1663,11 +1671,10 @@ invalid:
static json_object *
uc_require(uc_vm *vm, size_t nargs)
{
- const char *name = json_object_get_string(uc_get_arg(0));
-
json_object *val = uc_get_arg(0);
json_object *search, *se, *res;
size_t arridx, arrlen;
+ const char *name;
if (!json_object_is_type(val, json_type_string))
return NULL;
@@ -1882,8 +1889,7 @@ uc_replace_str(uc_vm *vm, json_object *str,
const char *subject, regmatch_t *pmatch, size_t plen,
char **sp, size_t *sl)
{
- const char *r = str ? json_object_get_string(str) : "null";
- const char *p = r;
+ const char *p, *r = str ? json_object_get_string(str) : "null";
bool esc = false;
int i;
@@ -2202,13 +2208,20 @@ uc_system(uc_vm *vm, size_t nargs)
{
json_object *cmdline = uc_get_arg(0);
json_object *timeout = uc_get_arg(1);
+ const char **arglist, *fn, *s;
sigset_t sigmask, sigomask;
- const char **arglist, *fn;
struct timespec ts;
int64_t tms;
+ int rc, len;
pid_t cld;
size_t i;
- int rc;
+
+ if (timeout && (!json_object_is_type(timeout, json_type_int) || json_object_get_int64(timeout) < 0)) {
+ uc_vm_raise_exception(vm, EXCEPTION_TYPE,
+ "Invalid timeout specified");
+
+ return NULL;
+ }
switch (json_object_get_type(cmdline)) {
case json_type_string:
@@ -2220,10 +2233,21 @@ uc_system(uc_vm *vm, size_t nargs)
break;
case json_type_array:
- arglist = xalloc(sizeof(*arglist) * (json_object_array_length(cmdline) + 1));
+ len = json_object_array_length(cmdline);
+
+ if (len == 0) {
+ uc_vm_raise_exception(vm, EXCEPTION_TYPE,
+ "Passed command array is empty");
- for (i = 0; i < json_object_array_length(cmdline); i++)
- arglist[i] = json_object_get_string(json_object_array_get_idx(cmdline, i));
+ return NULL;
+ }
+
+ arglist = xalloc(sizeof(*arglist) * (len + 1));
+
+ for (i = 0; i < len; i++) {
+ s = json_object_get_string(json_object_array_get_idx(cmdline, i));
+ arglist[i] = s ? s : "null";
+ }
arglist[i] = NULL;
@@ -2236,13 +2260,6 @@ uc_system(uc_vm *vm, size_t nargs)
return NULL;
}
- if (timeout && (!json_object_is_type(timeout, json_type_int) || json_object_get_int64(timeout) < 0)) {
- uc_vm_raise_exception(vm, EXCEPTION_TYPE,
- "Invalid timeout specified");
-
- return NULL;
- }
-
tms = timeout ? json_object_get_int64(timeout) : 0;
if (tms > 0) {
diff --git a/lib/fs.c b/lib/fs.c
index 6ea4ec1..7a33529 100644
--- a/lib/fs.c
+++ b/lib/fs.c
@@ -629,6 +629,7 @@ uc_fs_getcwd(uc_vm *vm, size_t nargs)
if (errno == ERANGE)
continue;
+ free(buf);
err_return(errno);
}
while (true);
diff --git a/main.c b/main.c
index 55c9dd8..c185524 100644
--- a/main.c
+++ b/main.c
@@ -197,7 +197,7 @@ static json_object *
parse_envfile(FILE *fp)
{
json_object *rv = NULL;
- enum json_tokener_error err;
+ enum json_tokener_error err = json_tokener_continue;
struct json_tokener *tok;
char buf[128];
size_t rlen;