From: Quentin Schulz <quentin.schulz@cherry.de>
To: Simon Glass <sjg@chromium.org>,
U-Boot Mailing List <u-boot@lists.denx.de>
Cc: Tom Rini <trini@konsulko.com>
Subject: Re: [PATCH v4 19/23] cmd: Fix memory-mapping in cmp command
Date: Tue, 3 Sep 2024 11:42:19 +0200 [thread overview]
Message-ID: <8ff14ff9-eadb-4d75-a7f4-49a548110899@cherry.de> (raw)
In-Reply-To: <20240901222634.460873-20-sjg@chromium.org>
Hi Simon,
On 9/2/24 12:26 AM, Simon Glass wrote:
> This unmaps a different address from what was mapped. Fix it.
>
> Signed-off-by: Simon Glass <sjg@chromium.org>
> ---
>
> (no changes since v1)
>
> cmd/mem.c | 26 +++++++++++++-------------
> 1 file changed, 13 insertions(+), 13 deletions(-)
>
> diff --git a/cmd/mem.c b/cmd/mem.c
> index 274348068c2..4d6fde28531 100644
> --- a/cmd/mem.c
> +++ b/cmd/mem.c
> @@ -245,7 +245,7 @@ static int do_mem_cmp(struct cmd_tbl *cmdtp, int flag, int argc,
> int size;
> int rcode = 0;
> const char *type;
> - const void *buf1, *buf2, *base;
> + const void *buf1, *buf2, *base, *ptr1, *ptr2;
> ulong word1, word2; /* 64-bit if MEM_SUPPORT_64BIT_DATA */
>
> if (argc != 4)
> @@ -270,22 +270,22 @@ static int do_mem_cmp(struct cmd_tbl *cmdtp, int flag, int argc,
> bytes = size * count;
> base = buf1 = map_sysmem(addr1, bytes);
"base" isn't changed in the rest of the code, so we could just reuse it
instead of declaring yet another variable.
> buf2 = map_sysmem(addr2, bytes);
We could also set ptr2 here... Allowing to avoid the diff from here to....
> - for (ngood = 0; ngood < count; ++ngood) {
> + for (ngood = 0, ptr1 = buf1, ptr2 = buf2; ngood < count; ++ngood) { > if (size == 4) {
> - word1 = *(u32 *)buf1;
> - word2 = *(u32 *)buf2;
> + word1 = *(u32 *)ptr1;
> + word2 = *(u32 *)ptr2;
> } else if (MEM_SUPPORT_64BIT_DATA && size == 8) {
> - word1 = *(ulong *)buf1;
> - word2 = *(ulong *)buf2;
> + word1 = *(ulong *)ptr1;
> + word2 = *(ulong *)ptr2;
> } else if (size == 2) {
> - word1 = *(u16 *)buf1;
> - word2 = *(u16 *)buf2;
> + word1 = *(u16 *)ptr1;
> + word2 = *(u16 *)ptr2;
> } else {
> - word1 = *(u8 *)buf1;
> - word2 = *(u8 *)buf2;
> + word1 = *(u8 *)ptr1;
> + word2 = *(u8 *)ptr2;
> }
> if (word1 != word2) {
> - ulong offset = buf1 - base;
> + ulong offset = ptr1 - base;
> printf("%s at 0x%08lx (%#0*lx) != %s at 0x%08lx (%#0*lx)\n",
> type, (ulong)(addr1 + offset), size, word1,
> type, (ulong)(addr2 + offset), size, word2);
> @@ -293,8 +293,8 @@ static int do_mem_cmp(struct cmd_tbl *cmdtp, int flag, int argc,
> break;
> }
>
> - buf1 += size;
> - buf2 += size;
> + ptr1 += size;
> + ptr2 += size;
>
here - making the commit all the more explicit (for me this commit is
basically only renaming a variable, since unmap_system doesn't appear in
the git context) - by only changing:
unmap_system(buf1);
unmap_system(buf2);
to
unmap_system(base);
unmap_system(ptr2);
I believe?
Additionally, my linter tells me that:
buf1 += size;
buf2 += size;
is undefined behavior:
arithOperationsOnVoidPointer: 'buf1' is of type 'const void *'. When
using void pointers in calculations, the behaviour is undefined.
I suggest the following:
buf1 = ((u8 *)buf1) + size;
buf2 = ((u8 *)buf2) + size;
since size seems to be size in bytes?
What do you think?
We already have test/cmd/mem.c is this something we can augment to test
the unmapping is proper too?
Cheers,
Quentin
next prev parent reply other threads:[~2024-09-03 9:42 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-09-01 22:26 [PATCH v4 00/23] Fix various bugs Simon Glass
2024-09-01 22:26 ` [PATCH v4 01/23] nvmxip: Drop the message on probe Simon Glass
2024-09-01 22:26 ` [PATCH v4 02/23] nvmxip: Avoid probing on boot Simon Glass
2024-09-02 16:10 ` Tom Rini
2024-09-10 18:41 ` Simon Glass
2024-09-12 15:59 ` Abdellatif El Khlifi
2024-09-01 22:26 ` [PATCH v4 03/23] test/py: Fix some pylint warnings in test_ut.py Simon Glass
2024-09-01 22:26 ` [PATCH v4 04/23] scripts: Update pylint.base Simon Glass
2024-09-01 22:26 ` [PATCH v4 05/23] bootstd: Create a function to reset USB Simon Glass
2024-09-01 22:26 ` [PATCH v4 06/23] usb: Drop old non-DM code Simon Glass
2024-09-01 22:26 ` [PATCH v4 07/23] log: Add a new log category for the console Simon Glass
2024-09-01 22:26 ` [PATCH v4 08/23] usb: Add DEV_FLAGS_DM to stdio for USB keyboard Simon Glass
2024-09-01 22:26 ` [PATCH v4 09/23] dm: usb: Deal with USB keyboard persisting across tests Simon Glass
2024-09-01 22:26 ` [PATCH v4 10/23] test: mbr: Adjust test to use lower-case hex Simon Glass
2024-09-01 22:26 ` [PATCH v4 11/23] test: mbr: Adjust test to drop 0x Simon Glass
2024-09-01 22:26 ` [PATCH v4 12/23] sandbox: Change the range used for memory-mapping tags Simon Glass
2024-09-01 22:26 ` [PATCH v4 13/23] sandbox: Update cpu to use logging Simon Glass
2024-09-01 22:26 ` [PATCH v4 14/23] sandbox: Unmap old tags Simon Glass
2024-09-01 22:26 ` [PATCH v4 15/23] sandbox: Add some debugging to pci_io Simon Glass
2024-09-01 22:26 ` [PATCH v4 16/23] sandbox: Implement reference counting for address mapping Simon Glass
2024-09-01 22:26 ` [PATCH v4 17/23] mmc: Use map_sysmem() with buffers in the mmc command Simon Glass
2024-09-01 22:26 ` [PATCH v4 18/23] read: Tidy up use of map_sysmem() in the read command Simon Glass
2024-09-01 22:26 ` [PATCH v4 19/23] cmd: Fix memory-mapping in cmp command Simon Glass
2024-09-03 9:42 ` Quentin Schulz [this message]
2024-09-20 15:59 ` Simon Glass
2024-09-01 22:26 ` [PATCH v4 20/23] test: mbr: Unmap the buffers after use Simon Glass
2024-09-01 22:26 ` [PATCH v4 21/23] test: mbr: Use a constant for the block size Simon Glass
2024-09-01 22:26 ` [PATCH v4 22/23] test: mbr: Use RAM for the buffers Simon Glass
2024-09-01 22:26 ` [PATCH v4 23/23] test: mbr: Drop a duplicate test Simon Glass
2024-09-19 0:01 ` [PATCH v4 00/23] Fix various bugs Tom Rini
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=8ff14ff9-eadb-4d75-a7f4-49a548110899@cherry.de \
--to=quentin.schulz@cherry.de \
--cc=sjg@chromium.org \
--cc=trini@konsulko.com \
--cc=u-boot@lists.denx.de \
/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