From nobody Mon May 6 02:06:47 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1488637069255924.8073727340225; Sat, 4 Mar 2017 06:17:49 -0800 (PST) Received: from localhost ([::1]:35527 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ckAVH-0001QX-Jl for importer@patchew.org; Sat, 04 Mar 2017 09:17:47 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40838) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ck7X2-0003Dl-1T for qemu-devel@nongnu.org; Sat, 04 Mar 2017 06:07:25 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ck7Wy-0003kW-Sg for qemu-devel@nongnu.org; Sat, 04 Mar 2017 06:07:24 -0500 Received: from mail-pg0-x244.google.com ([2607:f8b0:400e:c05::244]:34634) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1ck7Wy-0003kR-Ks for qemu-devel@nongnu.org; Sat, 04 Mar 2017 06:07:20 -0500 Received: by mail-pg0-x244.google.com with SMTP id s67so15125072pgb.1 for ; Sat, 04 Mar 2017 03:07:20 -0800 (PST) Received: from localhost.localdomain ([49.207.50.75]) by smtp.gmail.com with ESMTPSA id f62sm28676784pfg.48.2017.03.04.03.07.16 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 04 Mar 2017 03:07:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=8LO0r9Cb+A80BC59WLpiH0Kl0xTTncDntaTLTHgxHYk=; b=WqQuTbR+n/opPhbnFm1tzqUuNO9eOw3VW7qbSSVPVWRNL6waP8rfSCp9GTdUUmv635 d2Mcu5AAEMMwODHQWdzSKcB7xcQpvFNWIB8T33ipObS5TqkXmk+kWCZKXycQTyft7EpO 8y8BGuofeemhRve6PKNJQJDwV2V7WlBSAdbEc5lbau0nVAbUYuZSGFlY9hU0vBgkLmPt g6Ub52zfwh4M8B940wMmEtV9fbP1PeZ0mFbjQ/9fMI59Hk1rkhwlvVzjadlNXdQhDPTV CQp+j+cTSMDebgPmPlb10tyUoqspV8bOrYfRZ4MExEOcSOAyr6j4gnnrgNZo8Be5byNH iDsA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=8LO0r9Cb+A80BC59WLpiH0Kl0xTTncDntaTLTHgxHYk=; b=dLfwblmy36bLVnukIjkOqfRp86hPhv68rJaRWYi3VsDVLk1X6jIf+mzQj2wPRH2WdN C2CALP5CDSamIRkPHi3rTi1ITCDEU+YgCG7kB63dmi0I7vDs05/wpeozKvXIeJ66PQQ0 mD6oiYY0SzYwhI919/JHvUada4pC1p7EQV0ke0eIyiqoHER91nJulsXlOxxvWgwWtoiz uvOTJuBr6doUKgeeJgGdRA7pvrePFl6nUVtvOIDVY5U/przvsl7peYbJCC2af0J6+Tcq Pae393cdd2Wexd29tHoli0kzaBdhSvLMZ5gLbUUG3vtiVslTTjyymH5s2GfPT4G7v4eC l+0g== X-Gm-Message-State: AMke39lmrfhKaujdpHhFVUpLktNZJmJ5pb8bjYJ8Drkcr2geJUMoUWbf46HUmKWVfgurXg== X-Received: by 10.98.105.134 with SMTP id e128mr3105349pfc.19.1488625639388; Sat, 04 Mar 2017 03:07:19 -0800 (PST) From: Saurav Sachidanand To: qemu-devel@nongnu.org Date: Sat, 4 Mar 2017 11:06:58 +0000 Message-Id: <20170304110658.1402-1-sauravsachidanand@gmail.com> X-Mailer: git-send-email 2.12.0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2607:f8b0:400e:c05::244 X-Mailman-Approved-At: Sat, 04 Mar 2017 09:16:51 -0500 Subject: [Qemu-devel] [Qemu-trivial] [PATCH] util: Use g_malloc/g_free in envlist.c X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Saurav Sachidanand Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Change malloc/free to g_malloc/g_free in util/envlist.c, except for entry->env_var which is allocated using strdup(3). Remove NULL checks for pointers returned from g_malloc as it exits in case of failure. Update calls to envlist_create to reflect this. Free array returned by envlist_to_environ using g_free. Update comments to reflect change in semantics. Signed-off-by: Saurav Sachidanand --- bsd-user/main.c | 12 +++--------- linux-user/main.c | 7 ++----- util/envlist.c | 31 +++++++++++++------------------ 3 files changed, 18 insertions(+), 32 deletions(-) diff --git a/bsd-user/main.c b/bsd-user/main.c index 714a692e6f..4d715840ff 100644 --- a/bsd-user/main.c +++ b/bsd-user/main.c @@ -744,10 +744,7 @@ int main(int argc, char **argv) qemu_init_cpu_list(); module_call_init(MODULE_INIT_QOM); =20 - if ((envlist =3D envlist_create()) =3D=3D NULL) { - (void) fprintf(stderr, "Unable to allocate envlist\n"); - exit(1); - } + envlist =3D envlist_create(); =20 /* add current environment into the list */ for (wrk =3D environ; *wrk !=3D NULL; wrk++) { @@ -785,10 +782,7 @@ int main(int argc, char **argv) usage(); } else if (!strcmp(r, "ignore-environment")) { envlist_free(envlist); - if ((envlist =3D envlist_create()) =3D=3D NULL) { - (void) fprintf(stderr, "Unable to allocate envlist\n"); - exit(1); - } + envlist =3D envlist_create(); } else if (!strcmp(r, "U")) { r =3D argv[optind++]; if (envlist_unsetenv(envlist, r) !=3D 0) @@ -959,7 +953,7 @@ int main(int argc, char **argv) free(*wrk); } =20 - free(target_environ); + g_free(target_environ); =20 if (qemu_loglevel_mask(CPU_LOG_PAGE)) { qemu_log("guest_base 0x%lx\n", guest_base); diff --git a/linux-user/main.c b/linux-user/main.c index 10a3bb3a12..3542c899f6 100644 --- a/linux-user/main.c +++ b/linux-user/main.c @@ -4229,10 +4229,7 @@ int main(int argc, char **argv, char **envp) qemu_init_cpu_list(); module_call_init(MODULE_INIT_QOM); =20 - if ((envlist =3D envlist_create()) =3D=3D NULL) { - (void) fprintf(stderr, "Unable to allocate envlist\n"); - exit(EXIT_FAILURE); - } + envlist =3D envlist_create(); =20 /* add current environment into the list */ for (wrk =3D environ; *wrk !=3D NULL; wrk++) { @@ -4432,7 +4429,7 @@ int main(int argc, char **argv, char **envp) free(*wrk); } =20 - free(target_environ); + g_free(target_environ); =20 if (qemu_loglevel_mask(CPU_LOG_PAGE)) { qemu_log("guest_base 0x%lx\n", guest_base); diff --git a/util/envlist.c b/util/envlist.c index e86857e70a..bc21481b17 100644 --- a/util/envlist.c +++ b/util/envlist.c @@ -17,16 +17,14 @@ static int envlist_parse(envlist_t *envlist, const char *env, int (*)(envlist_t *, const char *)); =20 /* - * Allocates new envlist and returns pointer to that or - * NULL in case of error. + * Allocates new envlist and returns pointer to it. */ envlist_t * envlist_create(void) { envlist_t *envlist; =20 - if ((envlist =3D malloc(sizeof (*envlist))) =3D=3D NULL) - return (NULL); + envlist =3D g_malloc(sizeof (*envlist)); =20 QLIST_INIT(&envlist->el_entries); envlist->el_count =3D 0; @@ -49,9 +47,9 @@ envlist_free(envlist_t *envlist) QLIST_REMOVE(entry, ev_link); =20 free((char *)entry->ev_var); - free(entry); + g_free(entry); } - free(envlist); + g_free(envlist); } =20 /* @@ -156,15 +154,14 @@ envlist_setenv(envlist_t *envlist, const char *env) if (entry !=3D NULL) { QLIST_REMOVE(entry, ev_link); free((char *)entry->ev_var); - free(entry); + g_free(entry); } else { envlist->el_count++; } =20 - if ((entry =3D malloc(sizeof (*entry))) =3D=3D NULL) - return (errno); + entry =3D g_malloc(sizeof (*entry)); if ((entry->ev_var =3D strdup(env)) =3D=3D NULL) { - free(entry); + g_free(entry); return (errno); } QLIST_INSERT_HEAD(&envlist->el_entries, entry, ev_link); @@ -202,7 +199,7 @@ envlist_unsetenv(envlist_t *envlist, const char *env) if (entry !=3D NULL) { QLIST_REMOVE(entry, ev_link); free((char *)entry->ev_var); - free(entry); + g_free(entry); =20 envlist->el_count--; } @@ -212,12 +209,12 @@ envlist_unsetenv(envlist_t *envlist, const char *env) /* * Returns given envlist as array of strings (in same form that * global variable environ is). Caller must free returned memory - * by calling free(3) for each element and for the array. Returned - * array and given envlist are not related (no common references). + * by calling free(3) for each element, and g_free for the array. + * Returned array and given envlist are not related (no common + * references). * * If caller provides count pointer, number of items in array is - * stored there. In case of error, NULL is returned and no memory - * is allocated. + * stored there. */ char ** envlist_to_environ(const envlist_t *envlist, size_t *count) @@ -225,9 +222,7 @@ envlist_to_environ(const envlist_t *envlist, size_t *co= unt) struct envlist_entry *entry; char **env, **penv; =20 - penv =3D env =3D malloc((envlist->el_count + 1) * sizeof (char *)); - if (env =3D=3D NULL) - return (NULL); + penv =3D env =3D g_malloc((envlist->el_count + 1) * sizeof (char *)); =20 for (entry =3D envlist->el_entries.lh_first; entry !=3D NULL; entry =3D entry->ev_link.le_next) { --=20 2.12.0