This introduces obstack_strndup and changes gdb to use it.
Note that obstack_strndup works like savestring, and not exactly like
xstrndup. The difference is that obstack_strndup uses the passed-in
length, while xstrndup uses strnlen to choose the length.
gdb/ChangeLog
2019-08-06 Tom Tromey <tom@tromey.com>
* stabsread.c (patch_block_stabs, read_one_struct_field)
(read_enum_type): Use obstack_strndup.
* rust-exp.y (rust_parser::copy_name): Use obstack_strndup.
* gdb_obstack.h (obstack_strndup): Use obstack_strndup.
* dwarf2read.c (guess_full_die_structure_name)
(anonymous_struct_prefix): Use obstack_strndup.
* dbxread.c (cp_set_block_scope): Use obstack_strndup.
* c-exp.y (yylex): Use obstack_strndup.
* ada-exp.y (write_object_renaming, write_ambiguous_var)
(write_var_or_type): Use obstack_strndup.
+2019-08-06 Tom Tromey <tom@tromey.com>
+
+ * stabsread.c (patch_block_stabs, read_one_struct_field)
+ (read_enum_type): Use obstack_strndup.
+ * rust-exp.y (rust_parser::copy_name): Use obstack_strndup.
+ * gdb_obstack.h (obstack_strndup): Use obstack_strndup.
+ * dwarf2read.c (guess_full_die_structure_name)
+ (anonymous_struct_prefix): Use obstack_strndup.
+ * dbxread.c (cp_set_block_scope): Use obstack_strndup.
+ * c-exp.y (yylex): Use obstack_strndup.
+ * ada-exp.y (write_object_renaming, write_ambiguous_var)
+ (write_var_or_type): Use obstack_strndup.
+
2019-08-06 Tom Tromey <tom@tromey.com>
* symfile.c (reread_symbols): Use obstack_strdup.
if (orig_left_context == NULL)
orig_left_context = get_selected_block (NULL);
- name = (char *) obstack_copy0 (&temp_parse_space, renamed_entity,
- renamed_entity_len);
+ name = obstack_strndup (&temp_parse_space, renamed_entity,
+ renamed_entity_len);
ada_lookup_encoded_symbol (name, orig_left_context, VAR_DOMAIN, &sym_info);
if (sym_info.symbol == NULL)
error (_("Could not find renamed variable: %s"), ada_decode (name));
if (end == NULL)
end = renaming_expr + strlen (renaming_expr);
- index_name
- = (char *) obstack_copy0 (&temp_parse_space, renaming_expr,
- end - renaming_expr);
+ index_name = obstack_strndup (&temp_parse_space, renaming_expr,
+ end - renaming_expr);
renaming_expr = end;
ada_lookup_encoded_symbol (index_name, orig_left_context,
memset (sym, 0, sizeof (struct symbol));
SYMBOL_DOMAIN (sym) = UNDEF_DOMAIN;
- SYMBOL_LINKAGE_NAME (sym)
- = (const char *) obstack_copy0 (&temp_parse_space, name, len);
+ SYMBOL_LINKAGE_NAME (sym) = obstack_strndup (&temp_parse_space, name, len);
SYMBOL_LANGUAGE (sym) = language_ada;
write_exp_elt_opcode (par_state, OP_VAR_VALUE);
encoded_name = ada_encode (name0.ptr);
name_len = strlen (encoded_name);
- encoded_name
- = (char *) obstack_copy0 (&temp_parse_space, encoded_name, name_len);
+ encoded_name = obstack_strndup (&temp_parse_space, encoded_name, name_len);
for (depth = 0; depth < MAX_RENAMING_CHAIN_LENGTH; depth += 1)
{
int tail_index;
if (checkpoint > 0)
{
current.value.sval.ptr
- = (const char *) obstack_copy0 (&cpstate->expansion_obstack,
- current.value.sval.ptr,
- current.value.sval.length);
+ = obstack_strndup (&cpstate->expansion_obstack,
+ current.value.sval.ptr,
+ current.value.sval.length);
token_fifo[0] = current;
if (checkpoint > 1)
const char *name = SYMBOL_DEMANGLED_NAME (symbol);
unsigned int prefix_len = cp_entire_prefix_len (name);
- block_set_scope (block,
- (const char *) obstack_copy0 (obstack, name, prefix_len),
+ block_set_scope (block, obstack_strndup (obstack, name, prefix_len),
obstack);
}
}
if (actual_name_len > die_name_len + 2
&& actual_name[actual_name_len
- die_name_len - 1] == ':')
- name = (char *) obstack_copy0 (
+ name = obstack_strndup (
&objfile->per_bfd->storage_obstack,
actual_name, actual_name_len - die_name_len - 2);
}
return "";
struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
- return (char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
- DW_STRING (attr),
- &base[-1] - DW_STRING (attr));
+ return obstack_strndup (&objfile->per_bfd->storage_obstack,
+ DW_STRING (attr),
+ &base[-1] - DW_STRING (attr));
}
/* Return the name of the namespace/class that DIE is defined within,
string.size ());
}
+/* Duplicate the first N characters of STRING, returning a
+ \0-terminated string that's allocated on the obstack OBSTACKP.
+ Note that exactly N characters are copied, even if STRING is
+ shorter. */
+
+static inline char *
+obstack_strndup (struct obstack *obstackp, const char *string, size_t n)
+{
+ return (char *) obstack_copy0 (obstackp, string, n);
+}
+
/* An obstack that frees itself on scope exit. */
struct auto_obstack : obstack
{
const char *
rust_parser::copy_name (const char *name, int len)
{
- return (const char *) obstack_copy0 (&obstack, name, len);
+ return obstack_strndup (&obstack, name, len);
}
/* Helper function to make an stoken from a C string. */
SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
SYMBOL_SET_LINKAGE_NAME
- (sym, (char *) obstack_copy0 (&objfile->objfile_obstack,
- name, pp - name));
+ (sym, obstack_strndup (&objfile->objfile_obstack,
+ name, pp - name));
pp += 2;
if (*(pp - 1) == 'F' || *(pp - 1) == 'f')
{
struct gdbarch *gdbarch = get_objfile_arch (objfile);
fip->list->field.name
- = (const char *) obstack_copy0 (&objfile->objfile_obstack, *pp, p - *pp);
+ = obstack_strndup (&objfile->objfile_obstack, *pp, p - *pp);
*pp = p + 1;
/* This means we have a visibility for a field coming. */
p = *pp;
while (*p != ':')
p++;
- name = (char *) obstack_copy0 (&objfile->objfile_obstack, *pp, p - *pp);
+ name = obstack_strndup (&objfile->objfile_obstack, *pp, p - *pp);
*pp = p + 1;
n = read_huge_number (pp, ',', &nbits, 0);
if (nbits != 0)