mirror of
https://github.com/torvalds/linux.git
synced 2024-11-15 08:31:55 +00:00
6549a8c0c3
The current codebase makes use of the zero-length array language
extension to the C90 standard, but the preferred mechanism to declare
variable-length types such as these ones is a flexible array
member[1][2], introduced in C99:
struct foo {
int stuff;
struct boo array[];
};
By making use of the mechanism above, we will get a compiler warning in
case the flexible array does not occur last in the structure, which will
help us prevent some kind of undefined behavior bugs from being
inadvertently introduced[3] to the codebase from now on.
Also, notice that, dynamic memory allocations won't be affected by this
change:
"Flexible array members have incomplete type, and so the sizeof operator
may not be applied. As a quirk of the original implementation of
zero-length arrays, sizeof evaluates to zero."[1]
sizeof(flexible-array-member) triggers a warning because flexible array
members have incomplete type[1]. There are some instances of code in
which the sizeof operator is being incorrectly/erroneously applied to
zero-length arrays and the result is zero. Such instances may be hiding
some bugs. So, this work (flexible-array member conversions) will also
help to get completely rid of those sorts of issues.
This issue was found with the help of Coccinelle.
[1] https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html
[2] https://github.com/KSPP/linux/issues/21
[3] commit 7649773293
("cxgb3/l2t: Fix undefined behaviour")
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Gustavo A. R. Silva <gustavo@embeddedor.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: http://lore.kernel.org/lkml/20200515172926.GA31976@embeddedor
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
86 lines
1.7 KiB
C
86 lines
1.7 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
/*
|
|
* Simple pointer stack
|
|
*
|
|
* (c) 2010 Arnaldo Carvalho de Melo <acme@redhat.com>
|
|
*/
|
|
|
|
#include "pstack.h"
|
|
#include "debug.h"
|
|
#include <linux/kernel.h>
|
|
#include <linux/zalloc.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
struct pstack {
|
|
unsigned short top;
|
|
unsigned short max_nr_entries;
|
|
void *entries[];
|
|
};
|
|
|
|
struct pstack *pstack__new(unsigned short max_nr_entries)
|
|
{
|
|
struct pstack *pstack = zalloc((sizeof(*pstack) +
|
|
max_nr_entries * sizeof(void *)));
|
|
if (pstack != NULL)
|
|
pstack->max_nr_entries = max_nr_entries;
|
|
return pstack;
|
|
}
|
|
|
|
void pstack__delete(struct pstack *pstack)
|
|
{
|
|
free(pstack);
|
|
}
|
|
|
|
bool pstack__empty(const struct pstack *pstack)
|
|
{
|
|
return pstack->top == 0;
|
|
}
|
|
|
|
void pstack__remove(struct pstack *pstack, void *key)
|
|
{
|
|
unsigned short i = pstack->top, last_index = pstack->top - 1;
|
|
|
|
while (i-- != 0) {
|
|
if (pstack->entries[i] == key) {
|
|
if (i < last_index)
|
|
memmove(pstack->entries + i,
|
|
pstack->entries + i + 1,
|
|
(last_index - i) * sizeof(void *));
|
|
--pstack->top;
|
|
return;
|
|
}
|
|
}
|
|
pr_err("%s: %p not on the pstack!\n", __func__, key);
|
|
}
|
|
|
|
void pstack__push(struct pstack *pstack, void *key)
|
|
{
|
|
if (pstack->top == pstack->max_nr_entries) {
|
|
pr_err("%s: top=%d, overflow!\n", __func__, pstack->top);
|
|
return;
|
|
}
|
|
pstack->entries[pstack->top++] = key;
|
|
}
|
|
|
|
void *pstack__pop(struct pstack *pstack)
|
|
{
|
|
void *ret;
|
|
|
|
if (pstack->top == 0) {
|
|
pr_err("%s: underflow!\n", __func__);
|
|
return NULL;
|
|
}
|
|
|
|
ret = pstack->entries[--pstack->top];
|
|
pstack->entries[pstack->top] = NULL;
|
|
return ret;
|
|
}
|
|
|
|
void *pstack__peek(struct pstack *pstack)
|
|
{
|
|
if (pstack->top == 0)
|
|
return NULL;
|
|
return pstack->entries[pstack->top - 1];
|
|
}
|