fprintf: Add guard against unlikely overlapping copy

In cf459ca16f ("fprintf: Pretty print struct members that are pointers
to nameless structs") I added some recursive logic that theoretically
may end up doing an overlapping copy as reported by coverity:

  Error: OVERLAPPING_COPY: [#def19]
  dwarves-1.13/dwarves_fprintf.c:707: assign: Assigning: "name" = "namebfptr".
  dwarves-1.13/dwarves_fprintf.c:705: equal: "name" is equal to the address of "namebfptr".
  dwarves-1.13/dwarves_fprintf.c:705: overlapping_copy: In the call to function "snprintf", the arguments "name" and "namebfptr" may point to the same object.
  #  703|   			if (tag__is_struct(ptype) || tag__is_union(ptype) ||
  #  704|   			    tag__is_enumeration(ptype)) {
  #  705|-> 				snprintf(namebfptr, sizeof(namebfptr), "* %s", name);
  #  706|   				tconf.rel_offset = 1;
  #  707|

Look at cf459ca16f to see what this is about, but for now I'm just
checking if this is the case and adding a guard, at some point I'll
address this properly to allow for pointers to pointers to nameless
struct/union/enums.

Reported-by: William Cohen <wcohen@redhat.com>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
This commit is contained in:
Arnaldo Carvalho de Melo 2019-07-02 11:46:31 -03:00
parent e737976c09
commit 7b36fab5a8
1 changed files with 4 additions and 1 deletions

View File

@ -702,6 +702,8 @@ next_type:
}
if ((tag__is_struct(ptype) || tag__is_union(ptype) ||
tag__is_enumeration(ptype)) && type__name(tag__type(ptype), cu) == NULL) {
if (name == namebfptr)
goto out_type_not_found;
snprintf(namebfptr, sizeof(namebfptr), "* %s", name);
tconf.rel_offset = 1;
name = namebfptr;
@ -782,7 +784,8 @@ out:
return printed;
out_type_not_found:
printed = fprintf(fp, "%-*s %s", tconf.type_spacing, "<ERROR>", name);
printed = fprintf(fp, "%-*s%s> %s", tconf.type_spacing, "<ERROR",
name == namebfptr ? ": pointer to pointer to inner struct/union/enum?" : "", name);
goto out;
}