Re: Writing to a const pointer: is this supposed to happen?

From: Kars Mulder
Date: Wed Jun 24 2020 - 11:25:50 EST


On Wednesday, June 24, 2020 15:10 CEST, Greg Kroah-Hartman wrote:
> Have you hit any runtime issues with this code doing this? These
> strings should be held in writable memory, right? Or do you see a
> codepath where that is not the case?

I haven't ran into any issues with it; I was just looking at the code
as reference for a patch I'm working on.

I initially assumed that casting a const pointer to non-const and
writing to it would be undefined behaviour, but after reading through
the C99 standard I can't find an unambiguous statement whether it is
undefined behaviour even if the const pointer points to an object that
was originally non-const (like char* -> const char* -> char* casts); it
only says it is undefined behaviour if the object was defined with the
const qualifier.

I should probably stop bothering you with my newbie questions.