modpost: replace the use of NOFAIL() with xmalloc() etc.

I think x*alloc() functions are cleaner.

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
This commit is contained in:
Masahiro Yamada 2024-08-12 21:48:51 +09:00
parent a9d83d7478
commit 4c2598e3b6
4 changed files with 17 additions and 27 deletions

View File

@ -23,6 +23,7 @@
#include <hashtable.h> #include <hashtable.h>
#include <list.h> #include <list.h>
#include <xalloc.h>
#include "modpost.h" #include "modpost.h"
#include "../../include/linux/license.h" #include "../../include/linux/license.h"
@ -97,14 +98,6 @@ static inline bool strends(const char *str, const char *postfix)
return strcmp(str + strlen(str) - strlen(postfix), postfix) == 0; return strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
} }
void *do_nofail(void *ptr, const char *expr)
{
if (!ptr)
fatal("Memory allocation failure: %s.\n", expr);
return ptr;
}
char *read_text_file(const char *filename) char *read_text_file(const char *filename)
{ {
struct stat st; struct stat st;
@ -123,7 +116,7 @@ char *read_text_file(const char *filename)
exit(1); exit(1);
} }
buf = NOFAIL(malloc(st.st_size + 1)); buf = xmalloc(st.st_size + 1);
nbytes = st.st_size; nbytes = st.st_size;
@ -181,7 +174,7 @@ static struct module *new_module(const char *name, size_t namelen)
{ {
struct module *mod; struct module *mod;
mod = NOFAIL(malloc(sizeof(*mod) + namelen + 1)); mod = xmalloc(sizeof(*mod) + namelen + 1);
memset(mod, 0, sizeof(*mod)); memset(mod, 0, sizeof(*mod));
INIT_LIST_HEAD(&mod->exported_symbols); INIT_LIST_HEAD(&mod->exported_symbols);
@ -240,7 +233,7 @@ static inline unsigned int tdb_hash(const char *name)
**/ **/
static struct symbol *alloc_symbol(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)); memset(s, 0, sizeof(*s));
strcpy(s->name, name); strcpy(s->name, name);
@ -313,8 +306,7 @@ static void add_namespace(struct list_head *head, const char *namespace)
struct namespace_list *ns_entry; struct namespace_list *ns_entry;
if (!contains_namespace(head, namespace)) { if (!contains_namespace(head, namespace)) {
ns_entry = NOFAIL(malloc(sizeof(*ns_entry) + ns_entry = xmalloc(sizeof(*ns_entry) + strlen(namespace) + 1);
strlen(namespace) + 1));
strcpy(ns_entry->namespace, namespace); strcpy(ns_entry->namespace, namespace);
list_add_tail(&ns_entry->list, head); list_add_tail(&ns_entry->list, head);
} }
@ -369,7 +361,7 @@ static struct symbol *sym_add_exported(const char *name, struct module *mod,
s = alloc_symbol(name); s = alloc_symbol(name);
s->module = mod; s->module = mod;
s->is_gpl_only = gpl_only; s->is_gpl_only = gpl_only;
s->namespace = NOFAIL(strdup(namespace)); s->namespace = xstrdup(namespace);
list_add_tail(&s->list, &mod->exported_symbols); list_add_tail(&s->list, &mod->exported_symbols);
hash_add_symbol(s); hash_add_symbol(s);
@ -637,7 +629,7 @@ static void handle_symbol(struct module *mod, struct elf_info *info,
if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER) if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
break; break;
if (symname[0] == '.') { if (symname[0] == '.') {
char *munged = NOFAIL(strdup(symname)); char *munged = xstrdup(symname);
munged[0] = '_'; munged[0] = '_';
munged[1] = toupper(munged[1]); munged[1] = toupper(munged[1]);
symname = munged; symname = munged;
@ -1677,7 +1669,7 @@ void buf_write(struct buffer *buf, const char *s, int len)
{ {
if (buf->size - buf->pos < len) { if (buf->size - buf->pos < len) {
buf->size += len + SZ; 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); strncpy(buf->p + buf->pos, s, len);
buf->pos += len; buf->pos += len;
@ -1962,7 +1954,7 @@ static void write_if_changed(struct buffer *b, const char *fname)
if (st.st_size != b->pos) if (st.st_size != b->pos)
goto close_write; goto close_write;
tmp = NOFAIL(malloc(b->pos)); tmp = xmalloc(b->pos);
if (fread(tmp, 1, b->pos, file) != b->pos) if (fread(tmp, 1, b->pos, file) != b->pos)
goto free_write; goto free_write;
@ -2167,7 +2159,7 @@ int main(int argc, char **argv)
external_module = true; external_module = true;
break; break;
case 'i': case 'i':
dl = NOFAIL(malloc(sizeof(*dl))); dl = xmalloc(sizeof(*dl));
dl->file = optarg; dl->file = optarg;
list_add_tail(&dl->list, &dump_lists); list_add_tail(&dl->list, &dump_lists);
break; break;

View File

@ -65,12 +65,8 @@
#define TO_NATIVE(x) \ #define TO_NATIVE(x) \
(target_is_big_endian == host_is_big_endian ? x : bswap(x)) (target_is_big_endian == host_is_big_endian ? x : bswap(x))
#define NOFAIL(ptr) do_nofail((ptr), #ptr)
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
void *do_nofail(void *ptr, const char *expr);
struct buffer { struct buffer {
char *p; char *p;
int pos; int pos;

View File

@ -8,6 +8,8 @@
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
#include <limits.h> #include <limits.h>
#include <xalloc.h>
#include "modpost.h" #include "modpost.h"
/* /*
@ -305,7 +307,7 @@ static int parse_source_files(const char *objfile, struct md4_ctx *md)
const char *base; const char *base;
int dirlen, ret = 0, check_files = 0; int dirlen, ret = 0, check_files = 0;
cmd = NOFAIL(malloc(strlen(objfile) + sizeof("..cmd"))); cmd = xmalloc(strlen(objfile) + sizeof("..cmd"));
base = strrchr(objfile, '/'); base = strrchr(objfile, '/');
if (base) { if (base) {
@ -316,7 +318,7 @@ static int parse_source_files(const char *objfile, struct md4_ctx *md)
dirlen = 0; dirlen = 0;
sprintf(cmd, ".%s.cmd", objfile); sprintf(cmd, ".%s.cmd", objfile);
} }
dir = NOFAIL(malloc(dirlen + 1)); dir = xmalloc(dirlen + 1);
strncpy(dir, objfile, dirlen); strncpy(dir, objfile, dirlen);
dir[dirlen] = '\0'; dir[dirlen] = '\0';

View File

@ -4,7 +4,7 @@
* Helper functions for finding the symbol in an ELF which is "nearest" * Helper functions for finding the symbol in an ELF which is "nearest"
* to a given address. * to a given address.
*/ */
#include <xalloc.h>
#include "modpost.h" #include "modpost.h"
struct syminfo { struct syminfo {
@ -125,8 +125,8 @@ void symsearch_init(struct elf_info *elf)
{ {
unsigned int table_size = symbol_count(elf); unsigned int table_size = symbol_count(elf);
elf->symsearch = NOFAIL(malloc(sizeof(struct symsearch) + elf->symsearch = xmalloc(sizeof(struct symsearch) +
sizeof(struct syminfo) * table_size)); sizeof(struct syminfo) * table_size);
elf->symsearch->table_size = table_size; elf->symsearch->table_size = table_size;
symsearch_populate(elf, elf->symsearch->table, table_size); symsearch_populate(elf, elf->symsearch->table, table_size);