public inbox for linux-kselftest@vger.kernel.org
 help / color / mirror / Atom feed
From: Dan Carpenter <dan.carpenter@oracle.com>
To: dave.hansen@linux.intel.com
Cc: linux-kselftest@vger.kernel.org
Subject: [bug report] x86/pkeys: Add self-tests
Date: Mon, 7 Feb 2022 15:55:02 +0300	[thread overview]
Message-ID: <20220207125502.GA20855@kili> (raw)

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

                 reply	other threads:[~2022-02-07 13:28 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220207125502.GA20855@kili \
    --to=dan.carpenter@oracle.com \
    --cc=dave.hansen@linux.intel.com \
    --cc=linux-kselftest@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox