Const really is const

Hi all

This isn’t a question or problem, just an observation.

Often I will have data that is flagged as ‘const’ but there will be some
function that is privledged and casts away the const’ness. Under QNX V4
this works fine.

Under QNX 6/Neutrino/RTP this generated a SIGSEGV. I assume, it put that
data into a segment that is marked as read-only.

I like that. Just one more example of an OS that is watching my back for
me.


Bill Caroselli – 1(530) 510-7292
Q-TPS Consulting
QTPS@EarthLink.net

In article <9qp7bh$35u$1@inn.qnx.com>,
Bill Caroselli (Q-TPS) <qtps@earthlink.net> wrote:

This isn’t a question or problem, just an observation.

Often I will have data that is flagged as ‘const’ but there will be some
function that is privledged and casts away the const’ness. Under QNX V4
this works fine.

Under QNX 6/Neutrino/RTP this generated a SIGSEGV. I assume, it put that
data into a segment that is marked as read-only.

I like that. Just one more example of an OS that is watching my back for
me.

In the spirit of observations: Just note that all things declared
const can’t be “protected”, so be careful as you don’t want it to turn
into a false sense of security.

Greg Comeau export ETA: December
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware’s Libraries… Have you tried it?

“Greg Comeau” <comeau@panix.com> wrote

In article <9qp7bh$35u$> 1@inn.qnx.com> >,
Bill Caroselli (Q-TPS) <> qtps@earthlink.net> > wrote:
Often I will have data that is flagged as ‘const’ but there will be some
function that is privledged and casts away the const’ness. Under QNX V4
this works fine.

Under QNX 6/Neutrino/RTP this generated a SIGSEGV. I assume, it put that
data into a segment that is marked as read-only.

In the spirit of observations: Just note that all things declared
const can’t be “protected”, so be careful as you don’t want it to turn
into a false sense of security.

True. Thanks Greg. My understanding is that for this to happen the data
must be either global or local static AND const.

Is that a safe assumption? (Greg, I’m aware that you can’t answer for
someone else’s compiler. But that is my understanding of the rules. Am I
correct?)

\

Bill Caroselli – 1(530) 510-7292
Q-TPS Consulting
QTPS@EarthLink.net

“Bill Caroselli (Q-TPS)” <qtps@earthlink.net> wrote:

This isn’t a question or problem, just an observation.

Often I will have data that is flagged as ‘const’ but there will be some
function that is privledged and casts away the const’ness. Under QNX V4
this works fine.

Only if you compile -ms (which for some reason is the default). Compile
-mf to make it crash.

The small memory model has two segments – the read-only code segment
and the writable data segment. Since the code segment are not reachable
using near data pointers, all the data must go into the writable
segment. Even if you turn the two segments into one by linking flat,
the linker doesn’t know which pieces of data were originally declared as
const, and can’t move them into the read-only portion of the segment.

But if you compile -mf, the compiler assumes that you’re going to link
flag, and puts const data in the code segment. The contents of the
“code segment” of a flat executable are loaded into read-only pages of
memory.

Under QNX 6/Neutrino/RTP this generated a SIGSEGV. I assume, it put that
data into a segment that is marked as read-only.

I like that. Just one more example of an OS that is watching my back for
me.

Just don’t rely on that. Not all const data go into read-only memory.


Wojtek Lerch QNX Software Systems Ltd.

Thanks Wojtek.


Bill Caroselli – 1(530) 510-7292
Q-TPS Consulting
QTPS@EarthLink.net


“Wojtek Lerch” <wojtek_l@ottawa.com> wrote in message
news:9qphv6$l6f$1@nntp.qnx.com

“Bill Caroselli (Q-TPS)” <> qtps@earthlink.net> > wrote:
Often I will have data that is flagged as ‘const’ but there will be some
function that is privledged and casts away the const’ness. Under QNX V4
this works fine.

Only if you compile -ms (which for some reason is the default). Compile
-mf to make it crash.

The small memory model has two segments – the read-only code segment
and the writable data segment. Since the code segment are not reachable
using near data pointers, all the data must go into the writable
segment. Even if you turn the two segments into one by linking flat,
the linker doesn’t know which pieces of data were originally declared as
const, and can’t move them into the read-only portion of the segment.

But if you compile -mf, the compiler assumes that you’re going to link
flag, and puts const data in the code segment. The contents of the
“code segment” of a flat executable are loaded into read-only pages of
memory.

Under QNX 6/Neutrino/RTP this generated a SIGSEGV. I assume, it put
that
data into a segment that is marked as read-only.

I like that. Just one more example of an OS that is watching my back
for
me.

Just don’t rely on that. Not all const data go into read-only memory.

As commented on by Grag Comeau.

Or, as the kid at the back of the 11th grade chemistry class piped up - about
mid-April -
"Constants never vary, do they()"
(
) we never figured out if it was a question(?) or an epiphany(!) -
we couldn’t stop laughing!

Except in software, of course!
Phil

“Bill Caroselli (Q-TPS)” wrote:

Hi all

This isn’t a question or problem, just an observation.

Often I will have data that is flagged as ‘const’ but there will be some
function that is privledged and casts away the const’ness. Under QNX V4
this works fine.

Under QNX 6/Neutrino/RTP this generated a SIGSEGV. I assume, it put that
data into a segment that is marked as read-only.

I like that. Just one more example of an OS that is watching my back for
me.


Bill Caroselli – 1(530) 510-7292
Q-TPS Consulting
QTPS@EarthLink.net

Or, “Constants arn’t, variables don’t”

“Phil Olynyk” <pholynyk@home.com> wrote in message
news:3BD06EFC.303159C2@home.com

Or, as the kid at the back of the 11th grade chemistry class piped up -
about
mid-April -
"Constants never vary, do they()"
(
) we never figured out if it was a question(?) or an
epiphany(!) -
we couldn’t stop laughing!

Except in software, of course!
Phil

In article <9qphi8$99s$1@inn.qnx.com>,
Bill Caroselli (Q-TPS) <qtps@earthlink.net> wrote:

“Greg Comeau” <> comeau@panix.com> > wrote
In article <9qp7bh$35u$> 1@inn.qnx.com> >,
Bill Caroselli (Q-TPS) <> qtps@earthlink.net> > wrote:
Often I will have data that is flagged as ‘const’ but there will be some
function that is privledged and casts away the const’ness. Under QNX V4
this works fine.

Under QNX 6/Neutrino/RTP this generated a SIGSEGV. I assume, it put that
data into a segment that is marked as read-only.

In the spirit of observations: Just note that all things declared
const can’t be “protected”, so be careful as you don’t want it to turn
into a false sense of security.

True. Thanks Greg. My understanding is that for this to happen the data
must be either global or local static AND const.

Is that a safe assumption? (Greg, I’m aware that you can’t answer for
someone else’s compiler. But that is my understanding of the rules. Am I
correct?)

That’s a reasonable understanding, but as you note, the compiler
vendor and/or the options being use may not have that understanding
(at least at that moment), so whether it is safe or not depending
upon that.

Greg Comeau export ETA: December
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware’s Libraries… Have you tried it?

In article <3BD06EFC.303159C2@home.com>,
Phil Olynyk <pholynyk@home.com> wrote:

Or, as the kid at the back of the 11th grade chemistry class piped up - about
mid-April -
"Constants never vary, do they()"
(
) we never figured out if it was a question(?) or an epiphany(!) -
we couldn’t stop laughing!

Except in software, of course!

The truth of the matter is that that kid was either complete idiot
or a rather clever person. Note how many facts you were told
through school which were incorrect. And on the particulars of
constants, they are what you were told they were for what you were
doing. Anyway, I’ll just leave you with this oxymoron: const variables :slight_smile:

Greg Comeau export ETA: December
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware’s Libraries… Have you tried it?

In article <9qpree$f6f$1@inn.qnx.com>,
Bill Caroselli (Q-TPS) <qtps@earthlink.net> wrote:

Or, “Constants arn’t, variables don’t”

Exactly!

Greg Comeau export ETA: December
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware’s Libraries… Have you tried it?

“Greg Comeau” <comeau@panix.com> wrote

. . . Anyway, I’ll just leave you with this oxymoron: const variables > :slight_smile:

Almost every server I write uses constant varibales. More specifically,
‘const volatile’ data. I.E. Someone has access to a commonly mapped buffer
that they are not allowed to modify, but which may be modified while they
are lookiong at it by someone else.

\

Bill Caroselli – 1(530) 510-7292
Q-TPS Consulting
QTPS@EarthLink.net

In article <9qqebs$pjv$1@inn.qnx.com>,
Bill Caroselli (Q-TPS) <qtps@earthlink.net> wrote:

“Greg Comeau” <> comeau@panix.com> > wrote
. . . Anyway, I’ll just leave you with this oxymoron: const variables > :slight_smile:

Almost every server I write uses constant varibales. More specifically,
‘const volatile’ data. I.E. Someone has access to a commonly mapped buffer
that they are not allowed to modify, but which may be modified while they
are lookiong at it by someone else.

Sure, that’s common with “shared memory”.

Greg Comeau export ETA: December
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware’s Libraries… Have you tried it?