linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Phil Sutter <phil@nwl.cc>
To: "Daniel." <danielhilst@gmail.com>
Cc: "linux-c-programming@vger.kernel.org"
	<linux-c-programming@vger.kernel.org>
Subject: Re: How to check if char pointer is a constant!?
Date: Thu, 7 Jul 2016 22:26:21 +0200	[thread overview]
Message-ID: <20160707202621.GP620@orbyte.nwl.cc> (raw)
In-Reply-To: <CAF3SDA6G32Kub4rK5BSmx7TkWoXnGWW2ESyHnM_+3t-naVPDww@mail.gmail.com>

Hi,

On Thu, Jul 07, 2016 at 04:40:56PM -0300, Daniel. wrote:
> I have some function, say
> void empty(char *p) { p[0] = '\0'; }
> 
> If I call it like this empty("Hello") it will segfaults since "Hello"
> is put in readonly section of the program. Is there a way to check for
> this?! Maybe some nasty gcc extension!?

The problem here is upon calling the function, the const char *
parameter is implicitly casted to char *. So inside the function there
is no way to check this I'd say. You can call gcc with -Wwrite-strings
to have it generate a warning whenever a string constant is assigned to
a non-const char * variable.

HTH, Phil

  reply	other threads:[~2016-07-07 20:26 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-07-07 19:40 How to check if char pointer is a constant!? Daniel.
2016-07-07 20:26 ` Phil Sutter [this message]
2016-07-08 12:33   ` Daniel.
2016-07-08 12:59     ` Phil Sutter
2016-07-08 14:01       ` Daniel.

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=20160707202621.GP620@orbyte.nwl.cc \
    --to=phil@nwl.cc \
    --cc=danielhilst@gmail.com \
    --cc=linux-c-programming@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;
as well as URLs for NNTP newsgroup(s).