From: Doug Evans Date: Sun, 14 Dec 2008 20:51:04 +0000 (+0000) Subject: * utils.c (xmalloc,xcalloc,xstrdup): New fns. X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=bca929d3a66489b6b2aff2a9a81bc17853dff79e;p=binutils-gdb.git * utils.c (xmalloc,xcalloc,xstrdup): New fns. * server.h (ATTR_MALLOC): New macro. (xmalloc,xcalloc,xstrdup): Declare. * hostio.c: Replace malloc,calloc,strdup with xmalloc,xcalloc,xstrdup. * inferiors.c: Ditto. * linux-low.c: Ditto. * mem-break.c: Ditto. * regcache.c: Ditto. * remote-utils.c: Ditto. * server.c: Ditto. * target.c: Ditto. * win32-low.c: Ditto. --- diff --git a/gdb/gdbserver/ChangeLog b/gdb/gdbserver/ChangeLog index 9448e714bd1..7c4d0730f2b 100644 --- a/gdb/gdbserver/ChangeLog +++ b/gdb/gdbserver/ChangeLog @@ -1,3 +1,18 @@ +2008-12-13 Doug Evans + + * utils.c (xmalloc,xcalloc,xstrdup): New fns. + * server.h (ATTR_MALLOC): New macro. + (xmalloc,xcalloc,xstrdup): Declare. + * hostio.c: Replace malloc,calloc,strdup with xmalloc,xcalloc,xstrdup. + * inferiors.c: Ditto. + * linux-low.c: Ditto. + * mem-break.c: Ditto. + * regcache.c: Ditto. + * remote-utils.c: Ditto. + * server.c: Ditto. + * target.c: Ditto. + * win32-low.c: Ditto. + 2008-12-12 Doug Evans * linux-low.c (linux_wait_for_process): Don't clobber current_inferior diff --git a/gdb/gdbserver/hostio.c b/gdb/gdbserver/hostio.c index 712deb487e2..b3e26cd6d81 100644 --- a/gdb/gdbserver/hostio.c +++ b/gdb/gdbserver/hostio.c @@ -116,7 +116,7 @@ require_data (char *p, int p_len, char **data, int *data_len) { int input_index, output_index, escaped; - *data = malloc (p_len); + *data = xmalloc (p_len); output_index = 0; escaped = 0; @@ -295,7 +295,7 @@ handle_open (char *own_buf) } /* Record the new file descriptor. */ - new_fd = malloc (sizeof (struct fd_list)); + new_fd = xmalloc (sizeof (struct fd_list)); new_fd->fd = fd; new_fd->next = open_fds; open_fds = new_fd; @@ -323,7 +323,7 @@ handle_pread (char *own_buf, int *new_packet_len) return; } - data = malloc (len); + data = xmalloc (len); #ifdef HAVE_PREAD ret = pread (fd, data, len, offset); #else diff --git a/gdb/gdbserver/inferiors.c b/gdb/gdbserver/inferiors.c index f35cfa06b17..be946299de0 100644 --- a/gdb/gdbserver/inferiors.c +++ b/gdb/gdbserver/inferiors.c @@ -95,7 +95,7 @@ remove_inferior (struct inferior_list *list, void add_thread (unsigned long thread_id, void *target_data, unsigned int gdb_id) { - struct thread_info *new_thread = malloc (sizeof (*new_thread)); + struct thread_info *new_thread = xmalloc (sizeof (*new_thread)); memset (new_thread, 0, sizeof (*new_thread)); @@ -261,12 +261,12 @@ match_dll (struct inferior_list_entry *inf, void *arg) void loaded_dll (const char *name, CORE_ADDR base_addr) { - struct dll_info *new_dll = malloc (sizeof (*new_dll)); + struct dll_info *new_dll = xmalloc (sizeof (*new_dll)); memset (new_dll, 0, sizeof (*new_dll)); new_dll->entry.id = -1; - new_dll->name = strdup (name); + new_dll->name = xstrdup (name); new_dll->base_addr = base_addr; add_inferior_to_list (&all_dlls, &new_dll->entry); @@ -314,7 +314,7 @@ add_pid_to_list (struct inferior_list *list, unsigned long pid) { struct inferior_list_entry *new_entry; - new_entry = malloc (sizeof (struct inferior_list_entry)); + new_entry = xmalloc (sizeof (struct inferior_list_entry)); new_entry->id = pid; add_inferior_to_list (list, new_entry); } diff --git a/gdb/gdbserver/linux-low.c b/gdb/gdbserver/linux-low.c index 24ac4844d0c..120656a55e5 100644 --- a/gdb/gdbserver/linux-low.c +++ b/gdb/gdbserver/linux-low.c @@ -249,7 +249,7 @@ add_process (unsigned long pid) { struct process_info *process; - process = (struct process_info *) malloc (sizeof (*process)); + process = (struct process_info *) xmalloc (sizeof (*process)); memset (process, 0, sizeof (*process)); process->head.id = pid; @@ -1120,7 +1120,7 @@ linux_resume_one_process (struct inferior_list_entry *entry, || process->bp_reinsert != 0)) { struct pending_signals *p_sig; - p_sig = malloc (sizeof (*p_sig)); + p_sig = xmalloc (sizeof (*p_sig)); p_sig->prev = process->pending_signals; p_sig->signal = signal; if (info == NULL) @@ -1291,7 +1291,7 @@ linux_queue_one_thread (struct inferior_list_entry *entry) if (process->resume->sig != 0) { struct pending_signals *p_sig; - p_sig = malloc (sizeof (*p_sig)); + p_sig = xmalloc (sizeof (*p_sig)); p_sig->prev = process->pending_signals; p_sig->signal = process->resume->sig; memset (&p_sig->info, 0, sizeof (siginfo_t)); @@ -1527,7 +1527,7 @@ regsets_fetch_inferior_registers () continue; } - buf = malloc (regset->size); + buf = xmalloc (regset->size); #ifndef __sparc__ res = ptrace (regset->get_request, inferior_pid, 0, buf); #else @@ -1580,7 +1580,7 @@ regsets_store_inferior_registers () continue; } - buf = malloc (regset->size); + buf = xmalloc (regset->size); /* First fill the buffer with the current register set contents, in case there are any items in the kernel's regset that are @@ -1832,7 +1832,7 @@ linux_test_for_tracefork (void) { int child_pid, ret, status; long second_pid; - char *stack = malloc (STACK_SIZE * 4); + char *stack = xmalloc (STACK_SIZE * 4); linux_supports_tracefork_flag = 0; @@ -2212,6 +2212,6 @@ initialize_low (void) #ifdef HAVE_LINUX_REGSETS for (num_regsets = 0; target_regsets[num_regsets].size >= 0; num_regsets++) ; - disabled_regsets = malloc (num_regsets); + disabled_regsets = xmalloc (num_regsets); #endif } diff --git a/gdb/gdbserver/mem-break.c b/gdb/gdbserver/mem-break.c index 8f5872f284b..b4aeec2d6cd 100644 --- a/gdb/gdbserver/mem-break.c +++ b/gdb/gdbserver/mem-break.c @@ -55,7 +55,7 @@ set_breakpoint_at (CORE_ADDR where, int (*handler) (CORE_ADDR)) if (breakpoint_data == NULL) error ("Target does not support breakpoints."); - bp = malloc (sizeof (struct breakpoint)); + bp = xmalloc (sizeof (struct breakpoint)); memset (bp, 0, sizeof (struct breakpoint)); (*the_target->read_memory) (where, bp->old_data, diff --git a/gdb/gdbserver/regcache.c b/gdb/gdbserver/regcache.c index b5ec2159fc2..66022dd6a63 100644 --- a/gdb/gdbserver/regcache.c +++ b/gdb/gdbserver/regcache.c @@ -94,12 +94,12 @@ new_register_cache (void) if (register_bytes == 0) return NULL; /* The architecture hasn't been initialized yet. */ - regcache = malloc (sizeof (*regcache)); + regcache = xmalloc (sizeof (*regcache)); /* Make sure to zero-initialize the register cache when it is created, in case there are registers the target never fetches. This way they'll read as zero instead of garbage. */ - regcache->registers = calloc (1, register_bytes); + regcache->registers = xcalloc (1, register_bytes); if (regcache->registers == NULL) fatal ("Could not allocate register cache."); diff --git a/gdb/gdbserver/remote-utils.c b/gdb/gdbserver/remote-utils.c index d37d56dd4a7..aba2402084a 100644 --- a/gdb/gdbserver/remote-utils.c +++ b/gdb/gdbserver/remote-utils.c @@ -530,7 +530,7 @@ putpkt_binary (char *buf, int cnt) char buf3[1]; char *p; - buf2 = malloc (PBUFSIZ); + buf2 = xmalloc (PBUFSIZ); /* Copy the packet into buffer BUF2, encapsulating it and giving it a checksum. */ @@ -1178,7 +1178,7 @@ look_up_one_symbol (const char *name, CORE_ADDR *addrp) unsigned int mem_len; decode_m_packet (&own_buf[1], &mem_addr, &mem_len); - mem_buf = malloc (mem_len); + mem_buf = xmalloc (mem_len); if (read_inferior_memory (mem_addr, mem_buf, mem_len) == 0) convert_int_to_ascii (mem_buf, own_buf, mem_len); else @@ -1209,8 +1209,8 @@ look_up_one_symbol (const char *name, CORE_ADDR *addrp) decode_address (addrp, p, q - p); /* Save the symbol in our cache. */ - sym = malloc (sizeof (*sym)); - sym->name = strdup (name); + sym = xmalloc (sizeof (*sym)); + sym->name = xstrdup (name); sym->addr = *addrp; sym->next = symbol_cache; symbol_cache = sym; @@ -1221,7 +1221,7 @@ look_up_one_symbol (const char *name, CORE_ADDR *addrp) void monitor_output (const char *msg) { - char *buf = malloc (strlen (msg) * 2 + 2); + char *buf = xmalloc (strlen (msg) * 2 + 2); buf[0] = 'O'; hexify (buf + 1, msg, 0); @@ -1259,7 +1259,7 @@ xml_escape_text (const char *text) } /* Expand the result. */ - result = malloc (i + special + 1); + result = xmalloc (i + special + 1); for (i = 0, special = 0; text[i] != '\0'; i++) switch (text[i]) { diff --git a/gdb/gdbserver/server.c b/gdb/gdbserver/server.c index 33db8a78dcf..6321a3548cc 100644 --- a/gdb/gdbserver/server.c +++ b/gdb/gdbserver/server.c @@ -436,7 +436,7 @@ handle_search_memory (char *own_buf, int packet_len) CORE_ADDR found_addr; int cmd_name_len = sizeof ("qSearch:memory:") - 1; - pattern = malloc (packet_len); + pattern = xmalloc (packet_len); if (pattern == NULL) { error ("Unable to allocate memory to perform the search"); @@ -460,7 +460,7 @@ handle_search_memory (char *own_buf, int packet_len) if (search_space_len < search_buf_size) search_buf_size = search_space_len; - search_buf = malloc (search_buf_size); + search_buf = xmalloc (search_buf_size); if (search_buf == NULL) { free (pattern); @@ -575,7 +575,7 @@ handle_query (char *own_buf, int packet_len, int *new_packet_len_p) return; if (len > PBUFSIZ - 2) len = PBUFSIZ - 2; - spu_buf = malloc (len + 1); + spu_buf = xmalloc (len + 1); if (!spu_buf) return; @@ -604,7 +604,7 @@ handle_query (char *own_buf, int packet_len, int *new_packet_len_p) require_running (own_buf); strcpy (own_buf, "E00"); - spu_buf = malloc (packet_len - 15); + spu_buf = xmalloc (packet_len - 15); if (!spu_buf) return; if (decode_xfer_write (own_buf + 16, packet_len - 16, &annex, @@ -648,7 +648,7 @@ handle_query (char *own_buf, int packet_len, int *new_packet_len_p) more. */ if (len > PBUFSIZ - 2) len = PBUFSIZ - 2; - data = malloc (len + 1); + data = xmalloc (len + 1); n = (*the_target->read_auxv) (ofs, data, len + 1); if (n < 0) write_enn (own_buf); @@ -726,7 +726,7 @@ handle_query (char *own_buf, int packet_len, int *new_packet_len_p) for (dll_ptr = all_dlls.head; dll_ptr != NULL; dll_ptr = dll_ptr->next) total_len += 128 + 6 * strlen (((struct dll_info *) dll_ptr)->name); - document = malloc (total_len); + document = xmalloc (total_len); strcpy (document, "\n"); p = document + strlen (document); @@ -782,7 +782,7 @@ handle_query (char *own_buf, int packet_len, int *new_packet_len_p) return; if (len > PBUFSIZ - 2) len = PBUFSIZ - 2; - workbuf = malloc (len + 1); + workbuf = xmalloc (len + 1); if (!workbuf) return; @@ -895,7 +895,7 @@ handle_query (char *own_buf, int packet_len, int *new_packet_len_p) /* Handle "monitor" commands. */ if (strncmp ("qRcmd,", own_buf, 6) == 0) { - char *mon = malloc (PBUFSIZ); + char *mon = xmalloc (PBUFSIZ); int len = strlen (own_buf + 6); if ((len % 2) != 0 || unhexify (mon, own_buf + 6, len / 2) != len / 2) @@ -975,7 +975,7 @@ handle_v_cont (char *own_buf, char *status, int *signal) /* Allocate room for one extra action, for the default remain-stopped behavior; if no default action is in the list, we'll need the extra slot. */ - resume_info = malloc ((n + 1) * sizeof (resume_info[0])); + resume_info = xmalloc ((n + 1) * sizeof (resume_info[0])); default_action.thread = -1; default_action.leave_stopped = 1; @@ -1107,7 +1107,7 @@ handle_v_run (char *own_buf, char *status, int *signal) new_argc++; } - new_argv = calloc (new_argc + 2, sizeof (char *)); + new_argv = xcalloc (new_argc + 2, sizeof (char *)); i = 0; for (p = own_buf + strlen ("vRun;"); *p; p = next_p) { @@ -1119,7 +1119,7 @@ handle_v_run (char *own_buf, char *status, int *signal) new_argv[i] = NULL; else { - new_argv[i] = malloc (1 + (next_p - p) / 2); + new_argv[i] = xmalloc (1 + (next_p - p) / 2); unhexify (new_argv[i], p, (next_p - p) / 2); new_argv[i][(next_p - p) / 2] = '\0'; } @@ -1141,7 +1141,7 @@ handle_v_run (char *own_buf, char *status, int *signal) return 0; } - new_argv[0] = strdup (program_argv[0]); + new_argv[0] = xstrdup (program_argv[0]); } /* Free the old argv. */ @@ -1443,17 +1443,17 @@ main (int argc, char *argv[]) initialize_async_io (); initialize_low (); - own_buf = malloc (PBUFSIZ + 1); - mem_buf = malloc (PBUFSIZ); + own_buf = xmalloc (PBUFSIZ + 1); + mem_buf = xmalloc (PBUFSIZ); if (pid == 0 && *next_arg != NULL) { int i, n; n = argc - (next_arg - argv); - program_argv = malloc (sizeof (char *) * (n + 1)); + program_argv = xmalloc (sizeof (char *) * (n + 1)); for (i = 0; i < n; i++) - program_argv[i] = strdup (next_arg[i]); + program_argv[i] = xstrdup (next_arg[i]); program_argv[i] = NULL; /* Wait till we are at first instruction in program. */ diff --git a/gdb/gdbserver/server.h b/gdb/gdbserver/server.h index ca5530dea09..8c24c9d1bbc 100644 --- a/gdb/gdbserver/server.h +++ b/gdb/gdbserver/server.h @@ -70,6 +70,14 @@ extern void *memmem (const void *, size_t , const void *, size_t); #endif #endif +#ifndef ATTR_MALLOC +#if defined(__GNUC__) && (__GNUC__ >= 3) +#define ATTR_MALLOC __attribute__ ((__malloc__)) +#else +#define ATTR_MALLOC /* nothing */ +#endif +#endif + /* A type used for binary buffers. */ typedef unsigned char gdb_byte; @@ -265,6 +273,9 @@ char *target_signal_to_name (enum target_signal); /* Functions from utils.c */ +void *xmalloc (size_t) ATTR_MALLOC; +void *xcalloc (size_t, size_t) ATTR_MALLOC; +char *xstrdup (const char *) ATTR_MALLOC; void perror_with_name (char *string); void error (const char *string,...) ATTR_NORETURN ATTR_FORMAT (printf, 1, 2); void fatal (const char *string,...) ATTR_NORETURN ATTR_FORMAT (printf, 1, 2); diff --git a/gdb/gdbserver/target.c b/gdb/gdbserver/target.c index 86c62a318a7..fbc558c6f88 100644 --- a/gdb/gdbserver/target.c +++ b/gdb/gdbserver/target.c @@ -77,7 +77,7 @@ write_inferior_memory (CORE_ADDR memaddr, const unsigned char *myaddr, if (buffer != NULL) free (buffer); - buffer = malloc (len); + buffer = xmalloc (len); memcpy (buffer, myaddr, len); check_mem_write (memaddr, buffer, len); res = (*the_target->write_memory) (memaddr, buffer, len); @@ -106,6 +106,6 @@ mywait (char *statusp, int connected_wait) void set_target_ops (struct target_ops *target) { - the_target = (struct target_ops *) malloc (sizeof (*the_target)); + the_target = (struct target_ops *) xmalloc (sizeof (*the_target)); memcpy (the_target, target, sizeof (*the_target)); } diff --git a/gdb/gdbserver/utils.c b/gdb/gdbserver/utils.c index 88cc1e02304..e45542acf78 100644 --- a/gdb/gdbserver/utils.c +++ b/gdb/gdbserver/utils.c @@ -30,6 +30,63 @@ /* Generally useful subroutines used throughout the program. */ +static void malloc_failure (size_t size) ATTR_NORETURN; + +static void +malloc_failure (size_t size) +{ + fprintf (stderr, "gdbserver: ran out of memory while trying to allocate %lu bytes\n", + (unsigned long) size); + exit (1); +} + +/* Allocate memory without fail. + If malloc fails, this will print a message to stderr and exit. */ + +void * +xmalloc (size_t size) +{ + void *newmem; + + if (size == 0) + size = 1; + newmem = malloc (size); + if (!newmem) + malloc_failure (size); + + return newmem; +} + +/* Allocate memory without fail and set it to zero. + If malloc fails, this will print a message to stderr and exit. */ + +void * +xcalloc (size_t nelem, size_t elsize) +{ + void *newmem; + + if (nelem == 0 || elsize == 0) + nelem = elsize = 1; + + newmem = calloc (nelem, elsize); + if (!newmem) + malloc_failure (nelem * elsize); + + return newmem; +} + +/* Copy a string into a memory buffer. + If malloc fails, this will print a message to stderr and exit. */ + +char * +xstrdup (const char *s) +{ + char *ret = strdup (s); + if (ret == NULL) + malloc_failure (strlen (s) + 1); + return ret; +} + /* Print the system error message for errno, and also mention STRING as the file name for which the error was encountered. Then return to command level. */ diff --git a/gdb/gdbserver/win32-low.c b/gdb/gdbserver/win32-low.c index acc6341c71f..8704142bc97 100644 --- a/gdb/gdbserver/win32-low.c +++ b/gdb/gdbserver/win32-low.c @@ -176,7 +176,7 @@ child_add_thread (DWORD tid, HANDLE h) if ((th = thread_rec (tid, FALSE))) return th; - th = calloc (1, sizeof (*th)); + th = xcalloc (1, sizeof (*th)); th->tid = tid; th->h = h;