From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 24644 invoked by alias); 30 Nov 2008 21:59:17 -0000 Received: (qmail 24636 invoked by uid 22791); 30 Nov 2008 21:59:16 -0000 X-Spam-Check-By: sourceware.org Received: from smtp-out.google.com (HELO smtp-out.google.com) (216.239.45.13) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Sun, 30 Nov 2008 21:58:45 +0000 Received: from wpaz37.hot.corp.google.com (wpaz37.hot.corp.google.com [172.24.198.101]) by smtp-out.google.com with ESMTP id mAULwgo3007005 for ; Sun, 30 Nov 2008 13:58:43 -0800 Received: from localhost (ruffy.corp.google.com [172.18.118.116]) by wpaz37.hot.corp.google.com with ESMTP id mAULwf4x028310 for ; Sun, 30 Nov 2008 13:58:41 -0800 Received: by localhost (Postfix, from userid 67641) id 181201C7A0C; Sun, 30 Nov 2008 13:58:40 -0800 (PST) To: gdb@sourceware.org Subject: xmalloc for gdbserver? Message-Id: <20081130215841.181201C7A0C@localhost> Date: Sun, 30 Nov 2008 21:59:00 -0000 From: dje@google.com (Doug Evans) X-IsSubscribed: yes Mailing-List: contact gdb-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-owner@sourceware.org X-SW-Source: 2008-11/txt/msg00164.txt.bz2 I was going to submit this but then checked for other occurrences and found a lot. diff -u -p -r1.15 regcache.c --- regcache.c 8 Sep 2008 21:16:18 -0000 1.15 +++ regcache.c 30 Nov 2008 21:32:41 -0000 @@ -95,6 +95,8 @@ new_register_cache (void) return NULL; /* The architecture hasn't been initialized yet. */ regcache = malloc (sizeof (*regcache)); + if (regcache == NULL) + fatal ("Could not allocate register cache."); /* 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 Do we want to create versions of xmalloc, et.al. for gdbserver? I'll submit a patch for whichever way we choose, I just need to know which way to go. [I realize many of these are requests for small amounts, and (a) segv'ing will be rare, and (b) segv'ing in such a loaded system may be of lesser value, but still ...] There are cases where one doesn't want gdbserver to die because of a malloc failure, I wouldn't willy-nilly start replacing all calls to use xmalloc per se. gdbserver$ grep -A3 "malloc *(" *.c hostio.c: *data = malloc (p_len); hostio.c- hostio.c- output_index = 0; hostio.c- escaped = 0; -- hostio.c: new_fd = malloc (sizeof (struct fd_list)); hostio.c- new_fd->fd = fd; hostio.c- new_fd->next = open_fds; hostio.c- open_fds = new_fd; -- hostio.c: data = malloc (len); hostio.c-#ifdef HAVE_PREAD hostio.c- ret = pread (fd, data, len, offset); hostio.c-#else -- inferiors.c: struct thread_info *new_thread = malloc (sizeof (*new_thread)); inferiors.c- inferiors.c- memset (new_thread, 0, sizeof (*new_thread)); inferiors.c- -- inferiors.c: struct dll_info *new_dll = malloc (sizeof (*new_dll)); inferiors.c- memset (new_dll, 0, sizeof (*new_dll)); inferiors.c- inferiors.c- new_dll->entry.id = -1; -- inferiors.c: new_entry = malloc (sizeof (struct inferior_list_entry)); inferiors.c- new_entry->id = pid; inferiors.c- add_inferior_to_list (list, new_entry); inferiors.c-} -- linux-low.c: process = (struct process_info *) malloc (sizeof (*process)); linux-low.c- memset (process, 0, sizeof (*process)); linux-low.c- linux-low.c- process->head.id = pid; -- linux-low.c: p_sig = malloc (sizeof (*p_sig)); linux-low.c- p_sig->prev = process->pending_signals; linux-low.c- p_sig->signal = signal; linux-low.c- if (info == NULL) -- linux-low.c: p_sig = malloc (sizeof (*p_sig)); linux-low.c- p_sig->prev = process->pending_signals; linux-low.c- p_sig->signal = process->resume->sig; linux-low.c- memset (&p_sig->info, 0, sizeof (siginfo_t)); -- linux-low.c: buf = malloc (regset->size); linux-low.c-#ifndef __sparc__ linux-low.c- res = ptrace (regset->get_request, inferior_pid, 0, buf); linux-low.c-#else -- linux-low.c: buf = malloc (regset->size); linux-low.c- linux-low.c- /* First fill the buffer with the current register set contents, linux-low.c- in case there are any items in the kernel's regset that are -- linux-low.c: char *stack = malloc (STACK_SIZE * 4); linux-low.c- linux-low.c- linux_supports_tracefork_flag = 0; linux-low.c- -- linux-low.c: disabled_regsets = malloc (num_regsets); linux-low.c-#endif linux-low.c-} -- mem-break.c: bp = malloc (sizeof (struct breakpoint)); mem-break.c- memset (bp, 0, sizeof (struct breakpoint)); mem-break.c- mem-break.c- (*the_target->read_memory) (where, bp->old_data, -- regcache.c: regcache = malloc (sizeof (*regcache)); regcache.c- if (regcache == NULL) regcache.c- fatal ("Could not allocate register cache."); regcache.c- -- remote-utils.c: buf2 = malloc (PBUFSIZ); remote-utils.c- remote-utils.c- /* Copy the packet into buffer BUF2, encapsulating it remote-utils.c- and giving it a checksum. */ -- remote-utils.c: mem_buf = malloc (mem_len); remote-utils.c- if (read_inferior_memory (mem_addr, mem_buf, mem_len) == 0) remote-utils.c- convert_int_to_ascii (mem_buf, own_buf, mem_len); remote-utils.c- else -- remote-utils.c: sym = malloc (sizeof (*sym)); remote-utils.c- sym->name = strdup (name); remote-utils.c- sym->addr = *addrp; remote-utils.c- sym->next = symbol_cache; -- remote-utils.c: char *buf = malloc (strlen (msg) * 2 + 2); remote-utils.c- remote-utils.c- buf[0] = 'O'; remote-utils.c- hexify (buf + 1, msg, 0); -- remote-utils.c: result = malloc (i + special + 1); remote-utils.c- for (i = 0, special = 0; text[i] != '\0'; i++) remote-utils.c- switch (text[i]) remote-utils.c- { -- server.c: pattern = malloc (packet_len); server.c- if (pattern == NULL) server.c- { server.c- error ("Unable to allocate memory to perform the search"); -- server.c: search_buf = malloc (search_buf_size); server.c- if (search_buf == NULL) server.c- { server.c- free (pattern); -- server.c: spu_buf = malloc (len + 1); server.c- if (!spu_buf) server.c- return; server.c- -- server.c: spu_buf = malloc (packet_len - 15); server.c- if (!spu_buf) server.c- return; server.c- if (decode_xfer_write (own_buf + 16, packet_len - 16, &annex, -- server.c: data = malloc (len + 1); server.c- n = (*the_target->read_auxv) (ofs, data, len + 1); server.c- if (n < 0) server.c- write_enn (own_buf); -- server.c: document = malloc (total_len); server.c- strcpy (document, "\n"); server.c- p = document + strlen (document); server.c- -- server.c: char *mon = malloc (PBUFSIZ); server.c- int len = strlen (own_buf + 6); server.c- server.c- if ((len % 2) != 0 || unhexify (mon, own_buf + 6, len / 2) != len / 2) -- server.c: resume_info = malloc ((n + 1) * sizeof (resume_info[0])); server.c- server.c- default_action.thread = -1; server.c- default_action.leave_stopped = 1; -- server.c: new_argv[i] = malloc (1 + (next_p - p) / 2); server.c- unhexify (new_argv[i], p, (next_p - p) / 2); server.c- new_argv[i][(next_p - p) / 2] = '\0'; server.c- } -- server.c: own_buf = malloc (PBUFSIZ + 1); server.c: mem_buf = malloc (PBUFSIZ); server.c- server.c- if (pid == 0 && *next_arg != NULL) server.c- { -- server.c: program_argv = malloc (sizeof (char *) * (n + 1)); server.c- for (i = 0; i < n; i++) server.c- program_argv[i] = strdup (next_arg[i]); server.c- program_argv[i] = NULL; -- target.c: buffer = malloc (len); target.c- memcpy (buffer, myaddr, len); target.c- check_mem_write (memaddr, buffer, len); target.c- res = (*the_target->write_memory) (memaddr, buffer, len); -- target.c: the_target = (struct target_ops *) malloc (sizeof (*the_target)); target.c- memcpy (the_target, target, sizeof (*the_target)); target.c-} gdbserver$