[bind10-dev] arbitrary character and plain char

Michal 'vorner' Vaner michal.vaner at nic.cz
Mon Apr 30 07:37:57 UTC 2012


Hello

On Wed, Apr 25, 2012 at 06:56:57PM +0000, JINMEI Tatuya / 神明達哉 wrote:
> If it allows that, and if we store it in a plain char, it may or may
> not work correctly for systems where plain chars are signed.  "The C++
> Programming Language" (ISBN 978-0201700732) shows an example of
> assigning 1023 to a signed char, and states "Plausible results are 127
> and -1".  In the former case, we'll lose highest 1 bit at the point of
> assignment.

I agree that signed char can cause problems, but I believe it's the other way
around ‒ the \255 is not safe, but a binary byte with high value is (assuming we
never try to act as if it is a number).

If we have code:
char c;
read(0, &c, 1);
char c = d;
write(1, &d, 1);

It'll work even with bytes >127, the same thing as was put inside will be
output. I believe this is guaranteed by the fact that any data type is
pointer-aliasable to array of unsigned chars and we use the char only as a place
where we store data by a pointer here, not a variable with some actual
properties.

On the other side, if we have thing like:
char c = 255;
printf("%d\n", (int) c);
it might output 255, -1, 127 or actually anything else.


I don't disagree with the fact we should switch to arrays of unsigned chars, or
more correctly, uint8 variables. It's just I don't think mandating them to be
spelled as \255 will help in this regard anyhow (unless we really handle the
case of char = signed char and high bit somehow explicitly).

With regards

-- 
The human mind ordinarily operates at only ten percent of its capacity
-- the rest is overhead for the operating system

Michal 'vorner' Vaner
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: <https://lists.isc.org/pipermail/bind10-dev/attachments/20120430/bcc610ce/attachment.bin>


More information about the bind10-dev mailing list