[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Invalid use of const pointer?

From: Paul Smith
Subject: Re: Invalid use of const pointer?
Date: Sat, 08 Jan 2022 10:37:17 -0500
User-agent: Evolution 3.36.5-0ubuntu1

On Fri, 2022-01-07 at 18:28 -0500, Joe Filion wrote:
> Line 3094 of read.c is: 
> char *userend = strchr (name + 1, '/'); 
> The name parameter is a const pointer, so the overloaded version of
> strchr that takes a const pointer as the first parameter should also
> return a const pointer.

You might be thinking of C++.  GNU make is written in C, not C++, and
there's no such thing as an "overloaded" function in C.  There's only
one strchr() and it has this signature (according to the C standard):

  char *strchr(const char *s, int c);

>  But userend is not a const pointer and is used to modify the string
> later in the code. I’m a bit surprised the compiler allows this and I
> realize this could just be a misunderstanding of something on my
> part. Does anyone else find this construct suspicious?

It is kind of gross, yes.  It relies on some internal knowledge that,
in fact, the string passed in is never truly constant (that we never
pass a string constant to this function, that might have been stored in
read-only memory).

The const-correct way to write this code would be to allocate a new
string and modify that (or, rework the entire program to use the
equivalent of C++'s std::string_view), but the author of this code
(might be me, might be someone else: I didn't investigate) decided that
the quick and dirty way had enough benefits to outweigh the gross-ness.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]