mirror of
https://sourceware.org/git/binutils-gdb.git
synced 2024-12-21 04:42:53 +08:00
de07e349be
These just get in the way of auditing for erroneous usage of strdup and add a huge irregular surface of "ctf_malloc or malloc? ctf_free or free? ctf_strdup or strdup?" ctf_malloc and ctf_free usage has not reliably matched up for many years, if ever, making the whole game pointless. Go back to malloc, free, and strdup like everyone else: while we're at it, fix a bunch of places where we weren't properly checking for OOM. This changes the interface of ctf_cuname_set and ctf_parent_name_set, which could strdup but could not return errors (like ENOMEM). New in v4. include/ * ctf-api.h (ctf_cuname_set): Can now fail, returning int. (ctf_parent_name_set): Likewise. libctf/ * ctf-impl.h (ctf_alloc): Remove. (ctf_free): Likewise. (ctf_strdup): Likewise. * ctf-subr.c (ctf_alloc): Remove. (ctf_free): Likewise. * ctf-util.c (ctf_strdup): Remove. * ctf-create.c (ctf_serialize): Use malloc, not ctf_alloc; free, not ctf_free; strdup, not ctf_strdup. (ctf_dtd_delete): Likewise. (ctf_dvd_delete): Likewise. (ctf_add_generic): Likewise. (ctf_add_function): Likewise. (ctf_add_enumerator): Likewise. (ctf_add_member_offset): Likewise. (ctf_add_variable): Likewise. (membadd): Likewise. (ctf_compress_write): Likewise. (ctf_write_mem): Likewise. * ctf-decl.c (ctf_decl_push): Likewise. (ctf_decl_fini): Likewise. (ctf_decl_sprintf): Likewise. Check for OOM. * ctf-dump.c (ctf_dump_append): Use malloc, not ctf_alloc; free, not ctf_free; strdup, not ctf_strdup. (ctf_dump_free): Likewise. (ctf_dump): Likewise. * ctf-open.c (upgrade_types_v1): Likewise. (init_types): Likewise. (ctf_file_close): Likewise. (ctf_bufopen_internal): Likewise. Check for OOM. (ctf_parent_name_set): Likewise: report the OOM to the caller. (ctf_cuname_set): Likewise. (ctf_import): Likewise. * ctf-string.c (ctf_str_purge_atom_refs): Use malloc, not ctf_alloc; free, not ctf_free; strdup, not ctf_strdup. (ctf_str_free_atom): Likewise. (ctf_str_create_atoms): Likewise. (ctf_str_add_ref_internal): Likewise. (ctf_str_remove_ref): Likewise. (ctf_str_write_strtab): Likewise.
197 lines
4.0 KiB
C
197 lines
4.0 KiB
C
/* Simple subrs.
|
|
Copyright (C) 2019 Free Software Foundation, Inc.
|
|
|
|
This file is part of libctf.
|
|
|
|
libctf is free software; you can redistribute it and/or modify it under
|
|
the terms of the GNU General Public License as published by the Free
|
|
Software Foundation; either version 3, or (at your option) any later
|
|
version.
|
|
|
|
This program is distributed in the hope that it will be useful, but
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
See the GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program; see the file COPYING. If not see
|
|
<http://www.gnu.org/licenses/>. */
|
|
|
|
#include <ctf-impl.h>
|
|
#ifdef HAVE_MMAP
|
|
#include <sys/mman.h>
|
|
#endif
|
|
#include <sys/types.h>
|
|
#include <stdarg.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
|
|
int _libctf_version = CTF_VERSION; /* Library client version. */
|
|
int _libctf_debug = 0; /* Debugging messages enabled. */
|
|
|
|
/* Private, read-only mmap from a file, with fallback to copying.
|
|
|
|
No handling of page-offset issues at all: the caller must allow for that. */
|
|
|
|
_libctf_malloc_ void *
|
|
ctf_mmap (size_t length, size_t offset, int fd)
|
|
{
|
|
void *data;
|
|
|
|
#ifdef HAVE_MMAP
|
|
data = mmap (NULL, length, PROT_READ, MAP_PRIVATE, fd, offset);
|
|
if (data == MAP_FAILED)
|
|
data = NULL;
|
|
#else
|
|
if ((data = malloc (length)) != NULL)
|
|
{
|
|
if (ctf_pread (fd, data, length, offset) <= 0)
|
|
{
|
|
free (data);
|
|
data = NULL;
|
|
}
|
|
}
|
|
#endif
|
|
return data;
|
|
}
|
|
|
|
void
|
|
ctf_munmap (void *buf, size_t length _libctf_unused_)
|
|
{
|
|
#ifdef HAVE_MMAP
|
|
(void) munmap (buf, length);
|
|
#else
|
|
free (buf);
|
|
#endif
|
|
}
|
|
|
|
ssize_t
|
|
ctf_pread (int fd, void *buf, ssize_t count, off_t offset)
|
|
{
|
|
ssize_t len;
|
|
size_t acc = 0;
|
|
char *data = (char *) buf;
|
|
|
|
#ifdef HAVE_PREAD
|
|
while (count > 0)
|
|
{
|
|
errno = 0;
|
|
if (((len = pread (fd, data, count, offset)) < 0) &&
|
|
errno != EINTR)
|
|
return len;
|
|
if (errno == EINTR)
|
|
continue;
|
|
|
|
acc += len;
|
|
if (len == 0) /* EOF. */
|
|
return acc;
|
|
|
|
count -= len;
|
|
offset += len;
|
|
data += len;
|
|
}
|
|
return acc;
|
|
#else
|
|
off_t orig_off;
|
|
|
|
if ((orig_off = lseek (fd, 0, SEEK_CUR)) < 0)
|
|
return -1;
|
|
if ((lseek (fd, offset, SEEK_SET)) < 0)
|
|
return -1;
|
|
|
|
while (count > 0)
|
|
{
|
|
errno = 0;
|
|
if (((len = read (fd, data, count)) < 0) &&
|
|
errno != EINTR)
|
|
return len;
|
|
if (errno == EINTR)
|
|
continue;
|
|
|
|
acc += len;
|
|
if (len == 0) /* EOF. */
|
|
break;
|
|
|
|
count -= len;
|
|
data += len;
|
|
}
|
|
if ((lseek (fd, orig_off, SEEK_SET)) < 0)
|
|
return -1; /* offset is smashed. */
|
|
#endif
|
|
|
|
return acc;
|
|
}
|
|
|
|
const char *
|
|
ctf_strerror (int err)
|
|
{
|
|
return (const char *) (strerror (err));
|
|
}
|
|
|
|
/* Set the CTF library client version to the specified version. If version is
|
|
zero, we just return the default library version number. */
|
|
int
|
|
ctf_version (int version)
|
|
{
|
|
if (version < 0)
|
|
{
|
|
errno = EINVAL;
|
|
return -1;
|
|
}
|
|
|
|
if (version > 0)
|
|
{
|
|
/* Dynamic version switching is not presently supported. */
|
|
if (version != CTF_VERSION)
|
|
{
|
|
errno = ENOTSUP;
|
|
return -1;
|
|
}
|
|
ctf_dprintf ("ctf_version: client using version %d\n", version);
|
|
_libctf_version = version;
|
|
}
|
|
|
|
return _libctf_version;
|
|
}
|
|
|
|
void
|
|
libctf_init_debug (void)
|
|
{
|
|
static int inited;
|
|
if (!inited)
|
|
{
|
|
_libctf_debug = getenv ("LIBCTF_DEBUG") != NULL;
|
|
inited = 1;
|
|
}
|
|
}
|
|
|
|
void ctf_setdebug (int debug)
|
|
{
|
|
/* Ensure that libctf_init_debug() has been called, so that we don't get our
|
|
debugging-on-or-off smashed by the next call. */
|
|
|
|
libctf_init_debug();
|
|
_libctf_debug = debug;
|
|
ctf_dprintf ("CTF debugging set to %i\n", debug);
|
|
}
|
|
|
|
int ctf_getdebug (void)
|
|
{
|
|
return _libctf_debug;
|
|
}
|
|
|
|
_libctf_printflike_ (1, 2)
|
|
void ctf_dprintf (const char *format, ...)
|
|
{
|
|
if (_libctf_debug)
|
|
{
|
|
va_list alist;
|
|
|
|
va_start (alist, format);
|
|
fflush (stdout);
|
|
(void) fputs ("libctf DEBUG: ", stderr);
|
|
(void) vfprintf (stderr, format, alist);
|
|
va_end (alist);
|
|
}
|
|
}
|