public inbox for linux-kselftest@vger.kernel.org
 help / color / mirror / Atom feed
* [bug report] x86/pkeys: Add self-tests
@ 2022-02-07 12:55 Dan Carpenter
  0 siblings, 0 replies; only message in thread
From: Dan Carpenter @ 2022-02-07 12:55 UTC (permalink / raw)
  To: dave.hansen; +Cc: linux-kselftest

Hello Dave Hansen,

The patch 5f23f6d082a9: "x86/pkeys: Add self-tests" from Jul 29,
2016, leads to the following Smatch static checker warning:

	tools/testing/selftests/vm/protection_keys.c:647 record_pkey_malloc()
	warn: address of 'pkey_malloc_records[i]' is probably non-NULL

tools/testing/selftests/vm/protection_keys.c
    638 long nr_pkey_malloc_records;
    639 void record_pkey_malloc(void *ptr, long size, int prot)
    640 {
    641         long i;
    642         struct pkey_malloc_record *rec = NULL;
    643 
    644         for (i = 0; i < nr_pkey_malloc_records; i++) {
    645                 rec = &pkey_malloc_records[i];
    646                 /* find a free record */
--> 647                 if (rec)
    648                         break;
    649         }
    650         if (!rec) {

This code is supposed re-allocate memory.  If we run out, then allocate
2x the memory.  But it only works for the first allocation where
"pkey_malloc_records" is NULL.

For the following allocations it will just select &pkey_malloc_records[0]
and re-use that again.

    651                 /* every record is full */
    652                 size_t old_nr_records = nr_pkey_malloc_records;
    653                 size_t new_nr_records = (nr_pkey_malloc_records * 2 + 1);
    654                 size_t new_size = new_nr_records * sizeof(struct pkey_malloc_record);
    655                 dprintf2("new_nr_records: %zd\n", new_nr_records);
    656                 dprintf2("new_size: %zd\n", new_size);
    657                 pkey_malloc_records = realloc(pkey_malloc_records, new_size);
    658                 pkey_assert(pkey_malloc_records != NULL);
    659                 rec = &pkey_malloc_records[nr_pkey_malloc_records];
    660                 /*
    661                  * realloc() does not initialize memory, so zero it from
    662                  * the first new record all the way to the end.
    663                  */
    664                 for (i = 0; i < new_nr_records - old_nr_records; i++)
    665                         memset(rec + i, 0, sizeof(*rec));
    666         }
    667         dprintf3("filling malloc record[%d/%p]: {%p, %ld}\n",
    668                 (int)(rec - pkey_malloc_records), rec, ptr, size);
    669         rec->ptr = ptr;
    670         rec->size = size;
    671         rec->prot = prot;
    672         pkey_last_malloc_record = rec;
    673         nr_pkey_malloc_records++;
    674 }

regards,
dan carpenter

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-02-07 13:28 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-02-07 12:55 [bug report] x86/pkeys: Add self-tests Dan Carpenter

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox