* [PATCH 2/4] modpost: replace the use of NOFAIL() with xmalloc() etc.
2024-08-12 12:48 [PATCH 1/4] kbuild: split x*alloc() functions in kconfig to scripts/include/xalloc.h Masahiro Yamada
@ 2024-08-12 12:48 ` Masahiro Yamada
2024-08-24 7:57 ` Masahiro Yamada
2024-08-12 12:48 ` [PATCH 3/4] kallsyms: use xmalloc() and xrealloc() Masahiro Yamada
2024-08-12 12:48 ` [PATCH 4/4] fixdep: use xmalloc() Masahiro Yamada
2 siblings, 1 reply; 5+ messages in thread
From: Masahiro Yamada @ 2024-08-12 12:48 UTC (permalink / raw)
To: linux-kbuild
Cc: linux-kernel, Masahiro Yamada, Nathan Chancellor, Nicolas Schier
I think x*alloc() functions are cleaner.
Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
---
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 <hashtable.h>
#include <list.h>
+#include <xalloc.h>
#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 <errno.h>
#include <string.h>
#include <limits.h>
+
+#include <xalloc.h>
#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 <xalloc.h>
#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);
--
2.43.0
^ permalink raw reply related [flat|nested] 5+ messages in thread* [PATCH 3/4] kallsyms: use xmalloc() and xrealloc()
2024-08-12 12:48 [PATCH 1/4] kbuild: split x*alloc() functions in kconfig to scripts/include/xalloc.h Masahiro Yamada
2024-08-12 12:48 ` [PATCH 2/4] modpost: replace the use of NOFAIL() with xmalloc() etc Masahiro Yamada
@ 2024-08-12 12:48 ` Masahiro Yamada
2024-08-12 12:48 ` [PATCH 4/4] fixdep: use xmalloc() Masahiro Yamada
2 siblings, 0 replies; 5+ messages in thread
From: Masahiro Yamada @ 2024-08-12 12:48 UTC (permalink / raw)
To: linux-kbuild; +Cc: linux-kernel, Masahiro Yamada
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 <masahiroy@kernel.org>
---
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 <ctype.h>
#include <limits.h>
+#include <xalloc.h>
+
#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;
--
2.43.0
^ permalink raw reply related [flat|nested] 5+ messages in thread* [PATCH 4/4] fixdep: use xmalloc()
2024-08-12 12:48 [PATCH 1/4] kbuild: split x*alloc() functions in kconfig to scripts/include/xalloc.h Masahiro Yamada
2024-08-12 12:48 ` [PATCH 2/4] modpost: replace the use of NOFAIL() with xmalloc() etc Masahiro Yamada
2024-08-12 12:48 ` [PATCH 3/4] kallsyms: use xmalloc() and xrealloc() Masahiro Yamada
@ 2024-08-12 12:48 ` Masahiro Yamada
2 siblings, 0 replies; 5+ messages in thread
From: Masahiro Yamada @ 2024-08-12 12:48 UTC (permalink / raw)
To: linux-kbuild
Cc: linux-kernel, Masahiro Yamada, Nathan Chancellor, Nicolas Schier
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 <masahiroy@kernel.org>
---
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 <stdio.h>
#include <ctype.h>
+#include <xalloc.h>
+
static void usage(void)
{
fprintf(stderr, "Usage: fixdep <depfile> <target> <cmdline>\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);
--
2.43.0
^ permalink raw reply related [flat|nested] 5+ messages in thread