diff mbox

util/envlist: Fix 2 bugs in envlist_setenv and envlist_unsetenv

Message ID CY1PR17MB031324368F190B92EFA547ACDB730@CY1PR17MB0313.namprd17.prod.outlook.com
State New
Headers show

Commit Message

chaojianhu Jan. 22, 2017, 8:54 a.m. UTC
From: chaojianhu <chaojianhu@hotmail.com>

In envlist_setenv, if any malloc fails, there will be inconsistency on el_count.

And in envlist_unsetenv, if env is "", the strncmp will be passed, eventually 
wrong envlist_entry (shoud be the first one) will be  removed.

Finally, in envlist_parse, to delimit environments with ',' is wrong, since value
may contain ','. I suggest delete envlist_parse from the source code.

Reported-by: chaojianhu <chaojianhu@hotmail.com>
Signed-off-by: chaojianhu <chaojianhu@hotmail.com>

---
 util/envlist.c | 30 +++++++++++++++++-------------
 1 file changed, 17 insertions(+), 13 deletions(-)

Comments

no-reply@patchew.org Jan. 22, 2017, 9:06 a.m. UTC | #1
Hi,

Your series seems to have some coding style problems. See output below for
more information:

Subject: [Qemu-devel] [PATCH] util/envlist: Fix 2 bugs in envlist_setenv and envlist_unsetenv
Message-id: CY1PR17MB031324368F190B92EFA547ACDB730@CY1PR17MB0313.namprd17.prod.outlook.com
Type: series

=== TEST SCRIPT BEGIN ===
#!/bin/bash

BASE=base
n=1
total=$(git log --oneline $BASE.. | wc -l)
failed=0

# Useful git options
git config --local diff.renamelimit 0
git config --local diff.renames True

commits="$(git log --format=%H --reverse $BASE..)"
for c in $commits; do
    echo "Checking PATCH $n/$total: $(git log -n 1 --format=%s $c)..."
    if ! git show $c --format=email | ./scripts/checkpatch.pl --mailback -; then
        failed=1
        echo
    fi
    n=$((n+1))
done

exit $failed
=== TEST SCRIPT END ===

Updating 3c8cf5a9c21ff8782164d1def7f44bd888713384
Switched to a new branch 'test'
3d0c260 util/envlist: Fix 2 bugs in envlist_setenv and envlist_unsetenv

=== OUTPUT BEGIN ===
Checking PATCH 1/1: util/envlist: Fix 2 bugs in envlist_setenv and envlist_unsetenv...
ERROR: code indent should never use tabs
#29: FILE: util/envlist.c:133:
+^Istruct envlist_entry *entry = NULL, *entry_old = NULL;$

ERROR: code indent should never use tabs
#40: FILE: util/envlist.c:150:
+^Ifor (entry_old = envlist->el_entries.lh_first; entry_old != NULL;$

ERROR: code indent should never use tabs
#41: FILE: util/envlist.c:151:
+^I    entry_old = entry_old->ev_link.le_next) {$

ERROR: code indent should never use tabs
#42: FILE: util/envlist.c:152:
+^I^Iif (strncmp(entry_old->ev_var, env, envname_len) == 0)$

ERROR: braces {} are necessary for all arms of this statement
#42: FILE: util/envlist.c:152:
+		if (strncmp(entry_old->ev_var, env, envname_len) == 0)
[...]

ERROR: code indent should never use tabs
#61: FILE: util/envlist.c:163:
+^Iif (entry_old != NULL) {$

ERROR: code indent should never use tabs
#62: FILE: util/envlist.c:164:
+^I^IQLIST_REMOVE(entry_old, ev_link);$

ERROR: code indent should never use tabs
#63: FILE: util/envlist.c:165:
+^I^Ifree((char *)entry_old->ev_var);$

ERROR: code indent should never use tabs
#64: FILE: util/envlist.c:166:
+^I^Ifree(entry_old);$

ERROR: code indent should never use tabs
#65: FILE: util/envlist.c:167:
+^I} else {$

ERROR: code indent should never use tabs
#66: FILE: util/envlist.c:168:
+^I^Ienvlist->el_count++;$

ERROR: code indent should never use tabs
#67: FILE: util/envlist.c:169:
+^I}$

ERROR: code indent should never use tabs
#76: FILE: util/envlist.c:189:
+^Ienvname_len = strlen(env);$

ERROR: code indent should never use tabs
#77: FILE: util/envlist.c:190:
+^Iif(0 == envname_len)$

ERROR: space required before the open parenthesis '('
#77: FILE: util/envlist.c:190:
+	if(0 == envname_len)

ERROR: braces {} are necessary for all arms of this statement
#77: FILE: util/envlist.c:190:
+	if(0 == envname_len)
[...]

ERROR: code indent should never use tabs
#78: FILE: util/envlist.c:191:
+^I^Ireturn (EINVAL);$

ERROR: return is not a function, parentheses are not required
#78: FILE: util/envlist.c:191:
+		return (EINVAL);

total: 18 errors, 0 warnings, 63 lines checked

Your patch has style problems, please review.  If any of these errors
are false positives report them to the maintainer, see
CHECKPATCH in MAINTAINERS.

=== OUTPUT END ===

Test command exited with code: 1


---
Email generated automatically by Patchew [http://patchew.org/].
Please send your feedback to patchew-devel@freelists.org
diff mbox

Patch

diff --git a/util/envlist.c b/util/envlist.c
index e86857e..327478b 100644
--- a/util/envlist.c
+++ b/util/envlist.c
@@ -130,7 +130,7 @@  envlist_parse(envlist_t *envlist, const char *env,
 int
 envlist_setenv(envlist_t *envlist, const char *env)
 {
-	struct envlist_entry *entry = NULL;
+	struct envlist_entry *entry = NULL, *entry_old = NULL;
 	const char *eq_sign;
 	size_t envname_len;
 
@@ -147,26 +147,27 @@  envlist_setenv(envlist_t *envlist, const char *env)
 	 * we remove and release it before allocating a whole
 	 * new entry.
 	 */
-	for (entry = envlist->el_entries.lh_first; entry != NULL;
-	    entry = entry->ev_link.le_next) {
-		if (strncmp(entry->ev_var, env, envname_len) == 0)
+	for (entry_old = envlist->el_entries.lh_first; entry_old != NULL;
+	    entry_old = entry_old->ev_link.le_next) {
+		if (strncmp(entry_old->ev_var, env, envname_len) == 0)
 			break;
 	}
 
-	if (entry != NULL) {
-		QLIST_REMOVE(entry, ev_link);
-		free((char *)entry->ev_var);
-		free(entry);
-	} else {
-		envlist->el_count++;
-	}
-
 	if ((entry = malloc(sizeof (*entry))) == NULL)
 		return (errno);
 	if ((entry->ev_var = strdup(env)) == NULL) {
 		free(entry);
 		return (errno);
 	}
+
+	if (entry_old != NULL) {
+		QLIST_REMOVE(entry_old, ev_link);
+		free((char *)entry_old->ev_var);
+		free(entry_old);
+	} else {
+		envlist->el_count++;
+	}
+
 	QLIST_INSERT_HEAD(&envlist->el_entries, entry, ev_link);
 
 	return (0);
@@ -185,6 +186,10 @@  envlist_unsetenv(envlist_t *envlist, const char *env)
 	if ((envlist == NULL) || (env == NULL))
 		return (EINVAL);
 
+	envname_len = strlen(env);
+	if(0 == envname_len)
+		return (EINVAL);
+
 	/* env is not allowed to contain '=' */
 	if (strchr(env, '=') != NULL)
 		return (EINVAL);
@@ -193,7 +198,6 @@  envlist_unsetenv(envlist_t *envlist, const char *env)
 	 * Find out the requested entry and remove
 	 * it from the list.
 	 */
-	envname_len = strlen(env);
 	for (entry = envlist->el_entries.lh_first; entry != NULL;
 	    entry = entry->ev_link.le_next) {
 		if (strncmp(entry->ev_var, env, envname_len) == 0)