From patchwork Mon Aug 12 12:48:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 13760518 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BC1E717B4F6; Mon, 12 Aug 2024 12:49:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723466943; cv=none; b=dygEVxoarvQpp4UCVLkBXF5rgPbbCMjGLkoHs42qo4aQuhWatJERJqkXcvDEoCTpJedOGzZL2G5z8Vq2bIM6VI2P2vfs/Ano2MrRTkYjaIRcBMsrrFb3hotzlC8kFtCFr4gIN3GZ71mCUy2djL9LPcNmwMl73XzAqm7neK+U0OM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723466943; c=relaxed/simple; bh=Ye4kmdG3MnrVhtoYZ45dT1ihmCJkAh8t9Hvkm/sofMg=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=tAxaWYlBrVLUjvuX6V07yNyRIkPWATNdXoexqkUcyqCrDtumLer/WJVSQfuEm6FbIliabE+Iat4rKgrGaY5kZTN3U3Brh8KYI8Xbz0sesWIa4E2vZZgdmzbfoH9BO1IqaCbKczmDr8V3IRb0S2gGRE0q3FR0UQ4doHgGcy1WfYQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ha1Ykl0Q; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="ha1Ykl0Q" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2E1EDC32782; Mon, 12 Aug 2024 12:49:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1723466943; bh=Ye4kmdG3MnrVhtoYZ45dT1ihmCJkAh8t9Hvkm/sofMg=; h=From:To:Cc:Subject:Date:From; b=ha1Ykl0Q4b9T6NSxJnTl+rSYtuogmkW7D+tBb8db7inzMscEo6X22R0LeaoEZwBmx SsoW6OmTdoFIxwQiOWk1iIPNCeBiZo2uIQpNMPY48b78KG3uBU5XGnv2JkpI1eXvtk hUz7EV5tvdI4E5AdlogRXjL+RUgSTj/Y7VtBtnvm4N0OTBjWf7ZYb1As4zh9tMncMy fm+qdLa9pOtR1ovr+kd7KLP1pa4QkAX22dwxhicyqnKQ8xUj6uAmeRmXsNw9ZY2Vex ITDS9wHDkFuCAc/UDyG9OSRRV3yTzG/tJLXYLOUEJIurwP6QMDO5z6bGS1glthHqzg oUH2mRlm0oXwQ== From: Masahiro Yamada To: linux-kbuild@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Masahiro Yamada , Nathan Chancellor , Nicolas Schier Subject: [PATCH 1/4] kbuild: split x*alloc() functions in kconfig to scripts/include/xalloc.h Date: Mon, 12 Aug 2024 21:48:50 +0900 Message-ID: <20240812124858.2107328-1-masahiroy@kernel.org> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-kbuild@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 These functions will be useful for other host programs. Signed-off-by: Masahiro Yamada --- scripts/include/xalloc.h | 53 ++++++++++++++++++++++++++++++++++++ scripts/kconfig/confdata.c | 1 + scripts/kconfig/expr.c | 1 + scripts/kconfig/lexer.l | 1 + scripts/kconfig/lkc.h | 5 ---- scripts/kconfig/mconf.c | 1 + scripts/kconfig/menu.c | 1 + scripts/kconfig/nconf.c | 1 + scripts/kconfig/nconf.gui.c | 1 + scripts/kconfig/parser.y | 1 + scripts/kconfig/preprocess.c | 1 + scripts/kconfig/qconf.cc | 1 + scripts/kconfig/symbol.c | 1 + scripts/kconfig/util.c | 50 +--------------------------------- 14 files changed, 65 insertions(+), 54 deletions(-) create mode 100644 scripts/include/xalloc.h diff --git a/scripts/include/xalloc.h b/scripts/include/xalloc.h new file mode 100644 index 000000000000..cdadb07d0592 --- /dev/null +++ b/scripts/include/xalloc.h @@ -0,0 +1,53 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#ifndef XALLOC_H +#define XALLOC_H + +#include +#include + +static inline void *xmalloc(size_t size) +{ + void *p = malloc(size); + + if (!p) + exit(1); + return p; +} + +static inline void *xcalloc(size_t nmemb, size_t size) +{ + void *p = calloc(nmemb, size); + + if (!p) + exit(1); + return p; +} + +static inline void *xrealloc(void *p, size_t size) +{ + p = realloc(p, size); + if (!p) + exit(1); + return p; +} + +static inline char *xstrdup(const char *s) +{ + char *p = strdup(s); + + if (!p) + exit(1); + return p; +} + +static inline char *xstrndup(const char *s, size_t n) +{ + char *p = strndup(s, n); + + if (!p) + exit(1); + return p; +} + +#endif /* XALLOC_H */ diff --git a/scripts/kconfig/confdata.c b/scripts/kconfig/confdata.c index 76193ce5a792..d8849dfb06db 100644 --- a/scripts/kconfig/confdata.c +++ b/scripts/kconfig/confdata.c @@ -18,6 +18,7 @@ #include #include +#include #include "internal.h" #include "lkc.h" diff --git a/scripts/kconfig/expr.c b/scripts/kconfig/expr.c index c349da7fe3f8..a16451347f63 100644 --- a/scripts/kconfig/expr.c +++ b/scripts/kconfig/expr.c @@ -9,6 +9,7 @@ #include #include +#include #include "lkc.h" #define DEBUG_EXPR 0 diff --git a/scripts/kconfig/lexer.l b/scripts/kconfig/lexer.l index 8dd597c4710d..9c2cdfc33c6f 100644 --- a/scripts/kconfig/lexer.l +++ b/scripts/kconfig/lexer.l @@ -13,6 +13,7 @@ #include #include +#include #include "lkc.h" #include "preprocess.h" diff --git a/scripts/kconfig/lkc.h b/scripts/kconfig/lkc.h index 401bdf36323a..ddfb2b1cb737 100644 --- a/scripts/kconfig/lkc.h +++ b/scripts/kconfig/lkc.h @@ -53,11 +53,6 @@ static inline void xfwrite(const void *str, size_t len, size_t count, FILE *out) /* util.c */ unsigned int strhash(const char *s); const char *file_lookup(const char *name); -void *xmalloc(size_t size); -void *xcalloc(size_t nmemb, size_t size); -void *xrealloc(void *p, size_t size); -char *xstrdup(const char *s); -char *xstrndup(const char *s, size_t n); /* lexer.l */ int yylex(void); diff --git a/scripts/kconfig/mconf.c b/scripts/kconfig/mconf.c index 3887eac75289..84ea9215c0a7 100644 --- a/scripts/kconfig/mconf.c +++ b/scripts/kconfig/mconf.c @@ -20,6 +20,7 @@ #include #include +#include #include "lkc.h" #include "lxdialog/dialog.h" #include "mnconf-common.h" diff --git a/scripts/kconfig/menu.c b/scripts/kconfig/menu.c index 323cc0b62be6..dbf3b49eb0fb 100644 --- a/scripts/kconfig/menu.c +++ b/scripts/kconfig/menu.c @@ -9,6 +9,7 @@ #include #include +#include #include "lkc.h" #include "internal.h" diff --git a/scripts/kconfig/nconf.c b/scripts/kconfig/nconf.c index b91ca47e9e9a..063b4f7ccbdb 100644 --- a/scripts/kconfig/nconf.c +++ b/scripts/kconfig/nconf.c @@ -12,6 +12,7 @@ #include #include +#include #include "lkc.h" #include "mnconf-common.h" #include "nconf.h" diff --git a/scripts/kconfig/nconf.gui.c b/scripts/kconfig/nconf.gui.c index 25a7263ef3c8..72b605efe549 100644 --- a/scripts/kconfig/nconf.gui.c +++ b/scripts/kconfig/nconf.gui.c @@ -4,6 +4,7 @@ * * Derived from menuconfig. */ +#include #include "nconf.h" #include "lkc.h" diff --git a/scripts/kconfig/parser.y b/scripts/kconfig/parser.y index 61900feb4254..212e496ce9e2 100644 --- a/scripts/kconfig/parser.y +++ b/scripts/kconfig/parser.y @@ -11,6 +11,7 @@ #include #include +#include #include "lkc.h" #include "internal.h" #include "preprocess.h" diff --git a/scripts/kconfig/preprocess.c b/scripts/kconfig/preprocess.c index 67d1fb95c491..783abcaa5cc5 100644 --- a/scripts/kconfig/preprocess.c +++ b/scripts/kconfig/preprocess.c @@ -11,6 +11,7 @@ #include #include +#include #include "internal.h" #include "lkc.h" #include "preprocess.h" diff --git a/scripts/kconfig/qconf.cc b/scripts/kconfig/qconf.cc index 7d239c032b3d..379768699b57 100644 --- a/scripts/kconfig/qconf.cc +++ b/scripts/kconfig/qconf.cc @@ -22,6 +22,7 @@ #include +#include #include "lkc.h" #include "qconf.h" diff --git a/scripts/kconfig/symbol.c b/scripts/kconfig/symbol.c index 71502abd3b12..55c9eb30c006 100644 --- a/scripts/kconfig/symbol.c +++ b/scripts/kconfig/symbol.c @@ -9,6 +9,7 @@ #include #include +#include #include "internal.h" #include "lkc.h" diff --git a/scripts/kconfig/util.c b/scripts/kconfig/util.c index 696ff477671e..50698fff5b9d 100644 --- a/scripts/kconfig/util.c +++ b/scripts/kconfig/util.c @@ -9,6 +9,7 @@ #include #include +#include #include "lkc.h" unsigned int strhash(const char *s) @@ -102,52 +103,3 @@ char *str_get(const struct gstr *gs) { return gs->s; } - -void *xmalloc(size_t size) -{ - void *p = malloc(size); - if (p) - return p; - fprintf(stderr, "Out of memory.\n"); - exit(1); -} - -void *xcalloc(size_t nmemb, size_t size) -{ - void *p = calloc(nmemb, size); - if (p) - return p; - fprintf(stderr, "Out of memory.\n"); - exit(1); -} - -void *xrealloc(void *p, size_t size) -{ - p = realloc(p, size); - if (p) - return p; - fprintf(stderr, "Out of memory.\n"); - exit(1); -} - -char *xstrdup(const char *s) -{ - char *p; - - p = strdup(s); - if (p) - return p; - fprintf(stderr, "Out of memory.\n"); - exit(1); -} - -char *xstrndup(const char *s, size_t n) -{ - char *p; - - p = strndup(s, n); - if (p) - return p; - fprintf(stderr, "Out of memory.\n"); - exit(1); -} From patchwork Mon Aug 12 12:48:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 13760519 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1131517B515; Mon, 12 Aug 2024 12:49:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723466945; cv=none; b=ewMqQu5YR8tX+R7ZI/XWKw3H+C61qjs2eynE4+5MSAZxCxyVYn+5EZbcUcsZ2e2GbXquoMMcZLzwVOLKTzXjdUqFyVHhEfeE+/uir+oSZybEapn0v8Fqtb04pBXYPOf33aw56f6Mtw2gge1NeSy1H9ul8WPB+FsRBNPFII/fYg4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723466945; c=relaxed/simple; bh=BLXeB1ErP7FsWen/tkLkEVUKQQmwuds8LsOvl8m/2Cw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=UWMU9O5VTQK9oeEVk1GNv7zCdd1tZKGj3WY5gm0L8N9WKE/0p8Chb+8vy6q3SZrN+IUtp8y+F5z9bN2Z8lDGsi00edjZtfrILrsjnB5u2Rfll+vXXumwlNJo7YtqG4f6fnpNUsgaula5sChhuIk759m9POjQ66kGDgHt4UZfIGc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=NVUL+Qb4; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="NVUL+Qb4" Received: by smtp.kernel.org (Postfix) with ESMTPSA id BADCDC4AF0D; Mon, 12 Aug 2024 12:49:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1723466944; bh=BLXeB1ErP7FsWen/tkLkEVUKQQmwuds8LsOvl8m/2Cw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=NVUL+Qb4KMCiXslu95Q/rdtjTeZUG6VSZzUJkiH4qZAlV40jp390RcevE4fJ+PdBg 08zcPp1yfdO2OxY4lc5yIcItMgYFWuY6XLcQ+orDdspBsoMSni3PAclELRYBPv1wn0 keIv4kXCwBMet3PMC9rQb1xbfh6Xcno+hKx2uf20KqlUPFDlRDYR8nOpMOvc9dTSm2 XoaElTqFyBeRBn/moRsdMrS73IJc8oWZsJk2mThFe5BkUFcvITn+IJvfgfCEuYsfpV VH6uh0TgWecgF5Qn88O1dHH5nG3qgqVPJ1qWXivotsakdYZ4/LWP2kG0wTxoSmH8si wrZ1F9SEuL2GQ== From: Masahiro Yamada To: linux-kbuild@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Masahiro Yamada , Nathan Chancellor , Nicolas Schier Subject: [PATCH 2/4] modpost: replace the use of NOFAIL() with xmalloc() etc. Date: Mon, 12 Aug 2024 21:48:51 +0900 Message-ID: <20240812124858.2107328-2-masahiroy@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240812124858.2107328-1-masahiroy@kernel.org> References: <20240812124858.2107328-1-masahiroy@kernel.org> Precedence: bulk X-Mailing-List: linux-kbuild@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 I think x*alloc() functions are cleaner. Signed-off-by: Masahiro Yamada --- scripts/mod/modpost.c | 20 ++++++++++---------- scripts/mod/modpost.h | 2 -- scripts/mod/sumversion.c | 6 ++++-- scripts/mod/symsearch.c | 6 +++--- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c index d16d0ace2775..dfcf14f7e960 100644 --- a/scripts/mod/modpost.c +++ b/scripts/mod/modpost.c @@ -23,6 +23,7 @@ #include #include +#include #include "modpost.h" #include "../../include/linux/license.h" @@ -120,7 +121,7 @@ char *read_text_file(const char *filename) exit(1); } - buf = NOFAIL(malloc(st.st_size + 1)); + buf = xmalloc(st.st_size + 1); nbytes = st.st_size; @@ -178,7 +179,7 @@ static struct module *new_module(const char *name, size_t namelen) { struct module *mod; - mod = NOFAIL(malloc(sizeof(*mod) + namelen + 1)); + mod = xmalloc(sizeof(*mod) + namelen + 1); memset(mod, 0, sizeof(*mod)); INIT_LIST_HEAD(&mod->exported_symbols); @@ -237,7 +238,7 @@ static inline unsigned int tdb_hash(const char *name) **/ static struct symbol *alloc_symbol(const char *name) { - struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1)); + struct symbol *s = xmalloc(sizeof(*s) + strlen(name) + 1); memset(s, 0, sizeof(*s)); strcpy(s->name, name); @@ -310,8 +311,7 @@ static void add_namespace(struct list_head *head, const char *namespace) struct namespace_list *ns_entry; if (!contains_namespace(head, namespace)) { - ns_entry = NOFAIL(malloc(sizeof(*ns_entry) + - strlen(namespace) + 1)); + ns_entry = xmalloc(sizeof(*ns_entry) + strlen(namespace) + 1); strcpy(ns_entry->namespace, namespace); list_add_tail(&ns_entry->list, head); } @@ -366,7 +366,7 @@ static struct symbol *sym_add_exported(const char *name, struct module *mod, s = alloc_symbol(name); s->module = mod; s->is_gpl_only = gpl_only; - s->namespace = NOFAIL(strdup(namespace)); + s->namespace = xstrdup(namespace); list_add_tail(&s->list, &mod->exported_symbols); hash_add_symbol(s); @@ -622,7 +622,7 @@ static void handle_symbol(struct module *mod, struct elf_info *info, if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER) break; if (symname[0] == '.') { - char *munged = NOFAIL(strdup(symname)); + char *munged = xstrdup(symname); munged[0] = '_'; munged[1] = toupper(munged[1]); symname = munged; @@ -1662,7 +1662,7 @@ void buf_write(struct buffer *buf, const char *s, int len) { if (buf->size - buf->pos < len) { buf->size += len + SZ; - buf->p = NOFAIL(realloc(buf->p, buf->size)); + buf->p = xrealloc(buf->p, buf->size); } strncpy(buf->p + buf->pos, s, len); buf->pos += len; @@ -1947,7 +1947,7 @@ static void write_if_changed(struct buffer *b, const char *fname) if (st.st_size != b->pos) goto close_write; - tmp = NOFAIL(malloc(b->pos)); + tmp = xmalloc(b->pos); if (fread(tmp, 1, b->pos, file) != b->pos) goto free_write; @@ -2133,7 +2133,7 @@ int main(int argc, char **argv) external_module = true; break; case 'i': - dl = NOFAIL(malloc(sizeof(*dl))); + dl = xmalloc(sizeof(*dl)); dl->file = optarg; list_add_tail(&dl->list, &dump_lists); break; diff --git a/scripts/mod/modpost.h b/scripts/mod/modpost.h index 58197b34a3c8..cf04043c7e93 100644 --- a/scripts/mod/modpost.h +++ b/scripts/mod/modpost.h @@ -72,8 +72,6 @@ #endif -#define NOFAIL(ptr) do_nofail((ptr), #ptr) - #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) void *do_nofail(void *ptr, const char *expr); diff --git a/scripts/mod/sumversion.c b/scripts/mod/sumversion.c index dc4878502276..e7d2da45b0df 100644 --- a/scripts/mod/sumversion.c +++ b/scripts/mod/sumversion.c @@ -8,6 +8,8 @@ #include #include #include + +#include #include "modpost.h" /* @@ -305,7 +307,7 @@ static int parse_source_files(const char *objfile, struct md4_ctx *md) const char *base; int dirlen, ret = 0, check_files = 0; - cmd = NOFAIL(malloc(strlen(objfile) + sizeof("..cmd"))); + cmd = xmalloc(strlen(objfile) + sizeof("..cmd")); base = strrchr(objfile, '/'); if (base) { @@ -316,7 +318,7 @@ static int parse_source_files(const char *objfile, struct md4_ctx *md) dirlen = 0; sprintf(cmd, ".%s.cmd", objfile); } - dir = NOFAIL(malloc(dirlen + 1)); + dir = xmalloc(dirlen + 1); strncpy(dir, objfile, dirlen); dir[dirlen] = '\0'; diff --git a/scripts/mod/symsearch.c b/scripts/mod/symsearch.c index aa4ed51f9960..b9737b92f7f8 100644 --- a/scripts/mod/symsearch.c +++ b/scripts/mod/symsearch.c @@ -4,7 +4,7 @@ * Helper functions for finding the symbol in an ELF which is "nearest" * to a given address. */ - +#include #include "modpost.h" struct syminfo { @@ -125,8 +125,8 @@ void symsearch_init(struct elf_info *elf) { unsigned int table_size = symbol_count(elf); - elf->symsearch = NOFAIL(malloc(sizeof(struct symsearch) + - sizeof(struct syminfo) * table_size)); + elf->symsearch = xmalloc(sizeof(struct symsearch) + + sizeof(struct syminfo) * table_size); elf->symsearch->table_size = table_size; symsearch_populate(elf, elf->symsearch->table, table_size); From patchwork Mon Aug 12 12:48:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 13760520 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A243917BB21; Mon, 12 Aug 2024 12:49:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723466946; cv=none; b=JdR7UdRHiZbWDMKX9bAMJLQlpCJrx4dWfGBFN3PuOA5du+VheqETDsmUHSClcnYWca7GkNwDQVXZn/+PxZGslPvZmEaZFO0O19wOcKfpz4E/xLjF0k/2riIBTnQ6iKLL5loQATc1Nk8mzoKNawc9d39Fu7LGA4bXIJKZyS7etI0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723466946; c=relaxed/simple; bh=ztG2yE6SHjyu4DtnrnBDimJEjkWcjurLBkb9I9GPUzM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=TO0BJ/K5GLOoLycvJHmAj7v/XcUQ73h67S6pp/w8LVrlqb3jVczVCJgnGCZnrP6bH9LvWmrOo6eT0dsnPDSplnkAVDB2bI3XP0wONLoBZlIkdnPzXR2spMl/71AGSTH3Ckp01hoAPZfRFOz4zANpJ//Bz6AMtuq1kht/mD1+JC8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=qGRthPTS; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="qGRthPTS" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5E30EC4AF0F; Mon, 12 Aug 2024 12:49:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1723466946; bh=ztG2yE6SHjyu4DtnrnBDimJEjkWcjurLBkb9I9GPUzM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qGRthPTSs20hAyr/G8jZooEdzh+DZX4WB5g6Z16LsxJrw0OMDFJcdLwEMlI5bOeJ3 TdQMdL63+xfHoMnPAUz+6BTEIQetSRiDPVkKG1yd9Z5PHWtBfBU2B6AQvUWTFEBcUS zxHa+1NkND+ek3vQQyF6kMCPcdjzUumGghPWOUmdUfOBc/hbkm3jdatBXCH9XlXtt1 EcaXvEO60c0F851D8wxzlImXq9uHP9OcHnkdEdHev8GZ/7KLpyWz7F/iGdBssNaZaQ YFkaSH4gxT8Um0j1/K+K2kWqvLNL74hq7sZR2VKJTx6cE6XrkoFO+OOJuQp/6nFbHw NJlulEP8p2j6g== From: Masahiro Yamada To: linux-kbuild@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Masahiro Yamada Subject: [PATCH 3/4] kallsyms: use xmalloc() and xrealloc() Date: Mon, 12 Aug 2024 21:48:52 +0900 Message-ID: <20240812124858.2107328-3-masahiroy@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240812124858.2107328-1-masahiroy@kernel.org> References: <20240812124858.2107328-1-masahiroy@kernel.org> Precedence: bulk X-Mailing-List: linux-kbuild@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 When malloc() or realloc() fails, there is not much userspace programs can do. xmalloc() and xrealloc() are useful to bail out on a memory allocation failure. Signed-off-by: Masahiro Yamada --- scripts/kallsyms.c | 23 +++++------------------ 1 file changed, 5 insertions(+), 18 deletions(-) diff --git a/scripts/kallsyms.c b/scripts/kallsyms.c index 0ed873491bf5..53c433b2e591 100644 --- a/scripts/kallsyms.c +++ b/scripts/kallsyms.c @@ -28,6 +28,8 @@ #include #include +#include + #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0])) #define KSYM_NAME_LEN 512 @@ -171,12 +173,7 @@ static struct sym_entry *read_symbol(FILE *in, char **buf, size_t *buf_len) * compressed together */ len++; - sym = malloc(sizeof(*sym) + len + 1); - if (!sym) { - fprintf(stderr, "kallsyms failure: " - "unable to allocate required amount of memory\n"); - exit(EXIT_FAILURE); - } + sym = xmalloc(sizeof(*sym) + len + 1); sym->addr = addr; sym->len = len; sym->sym[0] = type; @@ -281,12 +278,7 @@ static void read_map(const char *in) if (table_cnt >= table_size) { table_size += 10000; - table = realloc(table, sizeof(*table) * table_size); - if (!table) { - fprintf(stderr, "out of memory\n"); - fclose(fp); - exit (1); - } + table = xrealloc(table, sizeof(*table) * table_size); } table[table_cnt++] = sym; @@ -413,12 +405,7 @@ static void write_src(void) /* table of offset markers, that give the offset in the compressed stream * every 256 symbols */ markers_cnt = (table_cnt + 255) / 256; - markers = malloc(sizeof(*markers) * markers_cnt); - if (!markers) { - fprintf(stderr, "kallsyms failure: " - "unable to allocate required memory\n"); - exit(EXIT_FAILURE); - } + markers = xmalloc(sizeof(*markers) * markers_cnt); output_label("kallsyms_names"); off = 0; From patchwork Mon Aug 12 12:48:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 13760521 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0932717C21C; Mon, 12 Aug 2024 12:49:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723466948; cv=none; b=RnOir28t/gDukHRD034s7oz+H4uWrVVL/oie20QZV9zqCXIFf5oLZY9cUiy33pInpLTZA1ENQHCRNzcczkcHJvYE8AI8+ZFsOXtNn2eDGoLulGYc5yg3wY/n7tFrbBkquwtvEbifSiVzclG5ztN3R0owNZ2YtfTf9f2LlPtQghY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723466948; c=relaxed/simple; bh=02Pj1beU0IRFdGsryWL1zfFXnXFEMSju0qFxRU+Flrw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=D4xtjYcdIloMv8ZNOYsS9z+1KpYpYqykRM3gIwLHCs0j2bB/oIr9HIMsU6Rx5hplt5yTcYCikiWRGP4a+Cpp0RCjQMZfkQSwkJkuLqejP77xfIdyd022yIGu0U0YBrk5v5cFK/CUG9W0iEcbWfBlBdTwk9F8UZ2Q1Eh/P65Dl2A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=XlaRR4CH; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="XlaRR4CH" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9D4B2C4AF15; Mon, 12 Aug 2024 12:49:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1723466947; bh=02Pj1beU0IRFdGsryWL1zfFXnXFEMSju0qFxRU+Flrw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XlaRR4CH5Ja+d1IbbJLDT7/BxemcE3PCobmiu41D4g3Z6FlQvSphLjOvwOF2yrdSn 9siTE0ItamINcjBiBs/nT2tAlj+WqK6PJNCSXrVWHJ8VZAteS9GqAifk3IqAnY2KrL qaiZZmPiDXcWVwe4h4M0jzy4jEKmKrM6BHk+zFeCzpdYh/zwD6MOg3g5+o2ZeBUTHg 43yl5jcxCubonO6De19sYy/pKklw6UD364beHQfwgD3EPe79SHJuAlGg4U7ZMYF3AW PI9lC4gx7dBer4q6D7AqhVSUiwtQeZHcroBM7h4DUi6oYPVLO8XROmytLb9vKmQ26j /EvRPOgj7O6Uw== From: Masahiro Yamada To: linux-kbuild@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Masahiro Yamada , Nathan Chancellor , Nicolas Schier Subject: [PATCH 4/4] fixdep: use xmalloc() Date: Mon, 12 Aug 2024 21:48:53 +0900 Message-ID: <20240812124858.2107328-4-masahiroy@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240812124858.2107328-1-masahiroy@kernel.org> References: <20240812124858.2107328-1-masahiroy@kernel.org> Precedence: bulk X-Mailing-List: linux-kbuild@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 When malloc() fails, there is not much userspace programs can do. xmalloc() is useful to bail out on a memory allocation failure. Signed-off-by: Masahiro Yamada --- scripts/basic/fixdep.c | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/scripts/basic/fixdep.c b/scripts/basic/fixdep.c index 84b6efa849f4..cdd5da7e009b 100644 --- a/scripts/basic/fixdep.c +++ b/scripts/basic/fixdep.c @@ -99,6 +99,8 @@ #include #include +#include + static void usage(void) { fprintf(stderr, "Usage: fixdep \n"); @@ -131,12 +133,9 @@ static unsigned int strhash(const char *str, unsigned int sz) static void add_to_hashtable(const char *name, int len, unsigned int hash, struct item *hashtab[]) { - struct item *aux = malloc(sizeof(*aux) + len); + struct item *aux; - if (!aux) { - perror("fixdep:malloc"); - exit(1); - } + aux = xmalloc(sizeof(*aux) + len); memcpy(aux->name, name, len); aux->len = len; aux->hash = hash; @@ -228,11 +227,7 @@ static void *read_file(const char *filename) perror(filename); exit(2); } - buf = malloc(st.st_size + 1); - if (!buf) { - perror("fixdep: malloc"); - exit(2); - } + buf = xmalloc(st.st_size + 1); if (read(fd, buf, st.st_size) != st.st_size) { perror("fixdep: read"); exit(2);