Skip to content

Commit

Permalink
rhashtable: avoid -Wrestrict warning on overlapping sprintf output
Browse files Browse the repository at this point in the history
sprintf() is declared with a restrict keyword to not allow input and
output to point to the same buffer:

lib/test_rhashtable.c: In function 'print_ht':
lib/test_rhashtable.c:504:4: error: 'sprintf' argument 3 overlaps destination object 'buff' [-Werror=restrict]
  504 |    sprintf(buff, "%s\nbucket[%d] -> ", buff, i);
      |    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
lib/test_rhashtable.c:489:7: note: destination object referenced by 'restrict'-qualified argument 1 was declared here
  489 |  char buff[512] = "";
      |       ^~~~

Rework this function to remember the last offset instead to
avoid the warning.

Signed-off-by: Arnd Bergmann <[email protected]>
Signed-off-by: David S. Miller <[email protected]>
  • Loading branch information
arndb authored and davem330 committed Mar 24, 2021
1 parent b7fbc88 commit 4adec7f
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/test_rhashtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ static unsigned int __init print_ht(struct rhltable *rhlt)
struct rhashtable *ht;
const struct bucket_table *tbl;
char buff[512] = "";
int offset = 0;
unsigned int i, cnt = 0;

ht = &rhlt->ht;
Expand All @@ -501,18 +502,18 @@ static unsigned int __init print_ht(struct rhltable *rhlt)
next = !rht_is_a_nulls(pos) ? rht_dereference(pos->next, ht) : NULL;

if (!rht_is_a_nulls(pos)) {
sprintf(buff, "%s\nbucket[%d] -> ", buff, i);
offset += sprintf(buff + offset, "\nbucket[%d] -> ", i);
}

while (!rht_is_a_nulls(pos)) {
struct rhlist_head *list = container_of(pos, struct rhlist_head, rhead);
sprintf(buff, "%s[[", buff);
offset += sprintf(buff + offset, "[[");
do {
pos = &list->rhead;
list = rht_dereference(list->next, ht);
p = rht_obj(ht, pos);

sprintf(buff, "%s val %d (tid=%d)%s", buff, p->value.id, p->value.tid,
offset += sprintf(buff + offset, " val %d (tid=%d)%s", p->value.id, p->value.tid,
list? ", " : " ");
cnt++;
} while (list);
Expand All @@ -521,7 +522,7 @@ static unsigned int __init print_ht(struct rhltable *rhlt)
next = !rht_is_a_nulls(pos) ?
rht_dereference(pos->next, ht) : NULL;

sprintf(buff, "%s]]%s", buff, !rht_is_a_nulls(pos) ? " -> " : "");
offset += sprintf(buff + offset, "]]%s", !rht_is_a_nulls(pos) ? " -> " : "");
}
}
printk(KERN_ERR "\n---- ht: ----%s\n-------------\n", buff);
Expand Down

0 comments on commit 4adec7f

Please sign in to comment.