public inbox for linux-man@vger.kernel.org
 help / color / mirror / Atom feed
From: Alejandro Colomar <colomar.6.4.3@gmail.com>
To: mtk.manpages@gmail.com
Cc: linux-man@vger.kernel.org, Alejandro Colomar <colomar.6.4.3@gmail.com>
Subject: [PATCH 7/7] qsort.3: Fix casts
Date: Sat,  5 Sep 2020 17:15:01 +0200	[thread overview]
Message-ID: <20200905151501.609036-8-colomar.6.4.3@gmail.com> (raw)
In-Reply-To: <20200905151501.609036-1-colomar.6.4.3@gmail.com>

`p1` (and `p2` too) is `const void *` and it comes from a
`const char **` (for legacy reasons, argv is not `const` but should be
treated as if it were).  That means, the ultimate `char` is `const`:
"a pointer to a pointer to a const char".

Let's see what is going on before the fix first, and then the fix.

Before the fix:

`(char *const *)` (I removed the space on purpose) casts `p1` to be
"a pointer to a const pointer to a non-const char".  That's clearly
not what it originally was.

Then we dereference, ending with a `char *const`, which is
"a const pointer to a non-const char".  But given that the pointer value
is passed to a function, `const` doesn't make sense there, because the
function will already take a copy of it, so it is impossible to modify
the pointer itself.

The fix:

`(const char **)` The only thing that is const is the ultimate `char`,
which is the only thing that matters, because it is the only thing
strcmp(3) has access to (everything else, i.e. the pointers, are
copies).

Then, after the dereference we end up with `const char *`, the type of
argv (more or less, as previously noted), which is also the type of the
arguments to strcmp(3).

Signed-off-by: Alejandro Colomar <colomar.6.4.3@gmail.com>
---
 man3/qsort.3 | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/man3/qsort.3 b/man3/qsort.3
index e1af43cf0..24f0b6c92 100644
--- a/man3/qsort.3
+++ b/man3/qsort.3
@@ -137,7 +137,7 @@ cmpstringp(const void *p1, const void *p2)
        pointers to char", but strcmp(3) arguments are "pointers
        to char", hence the following cast plus dereference */
 
-    return strcmp(* (char * const *) p1, * (char * const *) p2);
+    return strcmp(*(const char **) p1, *(const char **) p2);
 }
 
 int
-- 
2.28.0


  parent reply	other threads:[~2020-09-05 15:17 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-05 15:14 [PATCH 0/7] Remove and/or fix casts Alejandro Colomar
2020-09-05 15:14 ` [PATCH 1/7] sock_diag.7: Remove unneeded casts Alejandro Colomar
2020-09-05 15:14 ` [PATCH 2/7] pthread_sigmask.3: " Alejandro Colomar
2020-09-05 15:14 ` [PATCH 3/7] msgop.2: " Alejandro Colomar
2020-09-05 15:14 ` [PATCH 4/7] user_namespaces.7: Remove unneeded cast Alejandro Colomar
2020-09-05 15:14 ` [PATCH 5/7] dlopen.3: " Alejandro Colomar
2020-09-06 13:02   ` Michael Kerrisk (man-pages)
2020-09-06 13:22     ` Alejandro Colomar
2020-09-06 14:27       ` Alejandro Colomar
2020-09-06 15:00         ` Michael Kerrisk (man-pages)
2020-09-06 15:13       ` Michael Kerrisk (man-pages)
2020-09-06 16:43         ` Alejandro Colomar
2020-09-05 15:15 ` [PATCH 6/7] bsearch.3: Fix intermediate type and remove unneeded casts Alejandro Colomar
2020-09-05 15:15 ` Alejandro Colomar [this message]
2020-09-06 12:59 ` [PATCH 0/7] Remove and/or fix casts Michael Kerrisk (man-pages)
2020-09-06 13:02   ` Michael Kerrisk (man-pages)

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=20200905151501.609036-8-colomar.6.4.3@gmail.com \
    --to=colomar.6.4.3@gmail.com \
    --cc=linux-man@vger.kernel.org \
    --cc=mtk.manpages@gmail.com \
    /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