Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

To err is human, to forgive, beyond the scope of the Operating System.


devel / comp.lang.c / Re: How to add ssize_t a by size_t b?

SubjectAuthor
* How to add ssize_t a by size_t b?wij
+* Re: How to add ssize_t a by size_t b?Guillaume
|+* Re: How to add ssize_t a by size_t b?Scott Lurndal
||`* Re: How to add ssize_t a by size_t b?Keith Thompson
|| `* Re: How to add ssize_t a by size_t b?James Kuyper
||  `* Re: How to add ssize_t a by size_t b?Keith Thompson
||   `* Re: How to add ssize_t a by size_t b?Manfred
||    `- Re: How to add ssize_t a by size_t b?James Kuyper
|`* Re: How to add ssize_t a by size_t b?James Kuyper
| `- Re: How to add ssize_t a by size_t b?Tim Rentsch
+* Re: How to add ssize_t a by size_t b?Keith Thompson
|`- Re: How to add ssize_t a by size_t b?Tim Rentsch
+- Re: How to add ssize_t a by size_t b?Tim Rentsch
`- Re: How to add ssize_t a by size_t b?Tim Rentsch

1
How to add ssize_t a by size_t b?

<c64a2f9b-4745-42f4-9bd4-ce093fbc2d68n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18475&group=comp.lang.c#18475

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:622a:610:: with SMTP id z16mr14113923qta.101.1633102325115;
Fri, 01 Oct 2021 08:32:05 -0700 (PDT)
X-Received: by 2002:ac8:7748:: with SMTP id g8mr13706571qtu.281.1633102324916;
Fri, 01 Oct 2021 08:32:04 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Fri, 1 Oct 2021 08:32:04 -0700 (PDT)
Injection-Info: google-groups.googlegroups.com; posting-host=58.115.187.102; posting-account=QJ9iEwoAAACyjkKjQAWQOwSEULNvZZkc
NNTP-Posting-Host: 58.115.187.102
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <c64a2f9b-4745-42f4-9bd4-ce093fbc2d68n@googlegroups.com>
Subject: How to add ssize_t a by size_t b?
From: wyni...@gmail.com (wij)
Injection-Date: Fri, 01 Oct 2021 15:32:05 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 11
 by: wij - Fri, 1 Oct 2021 15:32 UTC

To simply the question of "a+b":

ssize_t add(ssize_t a, size_t b) {
if(a+b would overflow) { set errno=ERANGE; }
a+=b; // ?
return a;
}

Another example:
ssize_t a=SSIZE_T_MIN;
size_t b=SIZE_T_MAX;
a+=b; // Is this OK? Or, How the addition is done correctly?

Re: How to add ssize_t a by size_t b?

<sj7h4g$19gs$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18480&group=comp.lang.c#18480

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!Smk/rEQ09ac3o4C3SZ0ZJg.user.46.165.242.75.POSTED!not-for-mail
From: mess...@bottle.org (Guillaume)
Newsgroups: comp.lang.c
Subject: Re: How to add ssize_t a by size_t b?
Date: Fri, 1 Oct 2021 19:39:23 +0200
Organization: Aioe.org NNTP Server
Message-ID: <sj7h4g$19gs$1@gioia.aioe.org>
References: <c64a2f9b-4745-42f4-9bd4-ce093fbc2d68n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Info: gioia.aioe.org; logging-data="42524"; posting-host="Smk/rEQ09ac3o4C3SZ0ZJg.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.14.0
Content-Language: fr
X-Notice: Filtered by postfilter v. 0.9.2
 by: Guillaume - Fri, 1 Oct 2021 17:39 UTC

Le 01/10/2021 à 17:32, wij a écrit :
> To simply the question of "a+b":
>
> ssize_t add(ssize_t a, size_t b) {
> if(a+b would overflow) { set errno=ERANGE; }
> a+=b; // ?
> return a;
> }
>
> Another example:
> ssize_t a=SSIZE_T_MIN;
> size_t b=SIZE_T_MAX;
> a+=b; // Is this OK? Or, How the addition is done correctly?

ssize_t is not standard C. It's defined in POSIX.
While it's a signed integer, whereas size_t is unsigned, I don't know
how it relates to size_t on a given implementation in terms of width.
Mixing the two doesn't seem like a good idea. At least if you want
portability or even just want to know when a mixed sum is going to overflow.

With that said, a more general fact is that artihmetic operations
involving unsigned integers only can never "overflow" in C, because the
result is always modulo N: quoting the standard: "A computation
involving unsigned operands can never overflow, because a result
that cannot be represented by the resulting unsigned integer
type is reduced modulo the number that is one greater than the
largest value that can be represented by the resulting type."

Now, except for the case of all operands being unsigned, all other
combinations would yield an "undefined behavior", as said here: "If an
exceptional condition occurs during the evaluation of an expression
(that is, if the result is not mathematically defined or not in the
range of representable values for its type), the behavior is undefined."

In short, if 'a+b' can't be represented with the resulting type (which
you call "would overflow"), then it's 'a+b (mod 2^N)' with N being the
bit width of the resulting integer type, if both a and b are unsigned.
In any other case, including your case above, it's undefined behavior.

So, the result of adding a ssize_t integer to a size_t integer is
undefined per the standard if the sum "would overflow". All you can
safely do here is test before adding.

Note that while, on many platforms, the result will effectively be
computed modulo 2^N as with unsigned operands only (because it's often
the cheapest way of doing it with many ISAs), you can't count on this
according to the standard. Not just that, but the operation itself could
well raise an exception, even though it's uncommon. So ideally, for
portable code, you should test *first* and only issue the operation if
the result "would not overflow".

Re: How to add ssize_t a by size_t b?

<JtI5J.65761$ol1.6157@fx42.iad>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18482&group=comp.lang.c#18482

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!feeder1.feed.usenet.farm!feed.usenet.farm!peer01.ams4!peer.am4.highwinds-media.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx42.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: sco...@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: How to add ssize_t a by size_t b?
Newsgroups: comp.lang.c
References: <c64a2f9b-4745-42f4-9bd4-ce093fbc2d68n@googlegroups.com> <sj7h4g$19gs$1@gioia.aioe.org>
Lines: 30
Message-ID: <JtI5J.65761$ol1.6157@fx42.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Fri, 01 Oct 2021 18:31:05 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Fri, 01 Oct 2021 18:31:05 GMT
X-Received-Bytes: 1843
 by: Scott Lurndal - Fri, 1 Oct 2021 18:31 UTC

Guillaume <message@bottle.org> writes:
>Le 01/10/2021 à 17:32, wij a écrit :
>> To simply the question of "a+b":
>>
>> ssize_t add(ssize_t a, size_t b) {
>> if(a+b would overflow) { set errno=ERANGE; }
>> a+=b; // ?
>> return a;
>> }
>>
>> Another example:
>> ssize_t a=SSIZE_T_MIN;
>> size_t b=SIZE_T_MAX;
>> a+=b; // Is this OK? Or, How the addition is done correctly?
>
>ssize_t is not standard C. It's defined in POSIX.
>While it's a signed integer, whereas size_t is unsigned, I don't know
>how it relates to size_t on a given implementation in terms of width.

POSIX specifies that sizeof(ssize_t) == sizeof(size_t).

>Mixing the two doesn't seem like a good idea. At least if you want
>portability or even just want to know when a mixed sum is going to overflow.

The C rules on mixing signed and unsigned values are well defined.

If the operand that has unsigned integer type has rank greater
than or equal to the rank of the type of the other operand, the
operand with signed integer type is converted to the type of
the operand with unsigned integer type.

Re: How to add ssize_t a by size_t b?

<sj7ler$sk8$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18483&group=comp.lang.c#18483

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: jameskuy...@alumni.caltech.edu (James Kuyper)
Newsgroups: comp.lang.c
Subject: Re: How to add ssize_t a by size_t b?
Date: Fri, 1 Oct 2021 14:53:15 -0400
Organization: A noiseless patient Spider
Lines: 52
Message-ID: <sj7ler$sk8$1@dont-email.me>
References: <c64a2f9b-4745-42f4-9bd4-ce093fbc2d68n@googlegroups.com>
<sj7h4g$19gs$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 1 Oct 2021 18:53:15 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="ce90e47a3078ffa7a7de2bd88a807eec";
logging-data="29320"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+/IpyJz4wCeRT7B5I7pgxd48EQANtrNAM="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.13.0
Cancel-Lock: sha1:Vzj0WFN7AH8chvyKE6SQ5Swgh/A=
In-Reply-To: <sj7h4g$19gs$1@gioia.aioe.org>
Content-Language: en-US
 by: James Kuyper - Fri, 1 Oct 2021 18:53 UTC

On 10/1/21 1:39 PM, Guillaume wrote:
> Le 01/10/2021 à 17:32, wij a écrit :
>> To simply the question of "a+b":
>>
>> ssize_t add(ssize_t a, size_t b) {
>> if(a+b would overflow) { set errno=ERANGE; }
>> a+=b; // ?
>> return a;
>> }
>>
>> Another example:
>> ssize_t a=SSIZE_T_MIN;
>> size_t b=SIZE_T_MAX;
>> a+=b; // Is this OK? Or, How the addition is done correctly?
>
> ssize_t is not standard C. It's defined in POSIX.
> While it's a signed integer, whereas size_t is unsigned, I don't know
> how it relates to size_t on a given implementation in terms of width.

ssize_t is a signed integer type with the same width as size_t.
Therefore, they should have the same integer conversion rank. Since the
sign bit is included in the width, SSIZE_MAX is guaranteed to be smaller
than SIZE_MAX.

The usual arithmetic conversions apply (6.5.6p5). The ssize_t value is
first converted to size_t (6.3.1.8p1). That is a well-defined conversion
- negative ssize_t values have SIZE_MAX+1 added to them (6.3.1.3p2). The
sum is calculated using size_t math, which will produce what I assume is
the desired result even if a is negative, so long as it is smaller than b.

Upon assignment, the result is converted to ssize_t. Values greater than
SSIZE_MAX would not result in undefined behavior, as you suggested.
Instead, they would result in an implementation-defined value or the
raising of an implementation-defined signal (6.3.1.3p3). This code will
probably not work as desired on an implementation that chooses to raise
a signal, and the implementation-defined value is not guaranteed to be
the one that he wants. Therefore, setting errno would not be sufficient,
the final conversion must be actively prevented from occurring if it
would otherwise overflow.

I would write the function as:

size_t c = a + b;
if(c > SSIZE_MAX)
return overflow_value;
else
return c;

where overflow_value is whatever value he wants add() to return when
there's an overflow. Depending upon the intended use, it might be either
a constant, or a value calculated from the value of c, but either way it
must be a valid ssize_t value.

Re: How to add ssize_t a by size_t b?

<87y27ch2id.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18487&group=comp.lang.c#18487

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!paganini.bofh.team!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: How to add ssize_t a by size_t b?
Date: Fri, 01 Oct 2021 14:13:46 -0700
Organization: None to speak of
Lines: 44
Message-ID: <87y27ch2id.fsf@nosuchdomain.example.com>
References: <c64a2f9b-4745-42f4-9bd4-ce093fbc2d68n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="13acd1fe4bff95bcb7ffb2395063c869";
logging-data="20569"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/dhVN7+lbTsrveUM8TuRXk"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:e6++PLgEDgNsURcCxEXXLK4V7B0=
sha1:kzzuRW4RneywgcxUeKI4hb5tvf0=
 by: Keith Thompson - Fri, 1 Oct 2021 21:13 UTC

wij <wyniijj@gmail.com> writes:
> To simply the question of "a+b":
>
> ssize_t add(ssize_t a, size_t b) {
> if(a+b would overflow) { set errno=ERANGE; }
> a+=b; // ?
> return a;
> }
>
> Another example:
> ssize_t a=SSIZE_T_MIN;
> size_t b=SIZE_T_MAX;
> a+=b; // Is this OK? Or, How the addition is done correctly?

In standard C, you don't, because ssize_t doesn't exist (unless you
define it yourself).

However, POSIX does define a type ssize_t, which is a signed integer
type that can represent values up to SSIZE_MAX.

The rules are given in N1570 6.3.1.8, "Usual arithmetic conversions".

It's likely, but not guaranteed, that size_t and ssize_t have the same
rank. If so, then the ssize_t value is converted to size_t, and
assuming SSIZE_MAX <= SIZE_MAX the conversion does not change the
value. The addition is then done with two size_t (unsigned) operands,
with the usual wraparound semantics if the result exceeds SIZE_MAX.

It's very likely that ssize_t is the signed type that corresponds to the
unsigned type size_t; for example, if size_t is unsigned long, then
ssize_t is probably long. But I don't think POSIX actually guarantees
that. (Which means, for example, that a "%zd" format specifier isn't
guaranteed to be correct for value of type ssize_t.)

This assumes that size_t and ssize_t have a rank greater than or equal
to that of int, which is not guaranteed but is almost certain. If not
then both operands are promoted via the "integer promotions" before the
"usual arithmetic conversions" are applied. (This is relevant if, for
example, you want to add operands of types short and unsigned short.)

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips
void Void(void) { Void(); } /* The recursive call of the void */

Re: How to add ssize_t a by size_t b?

<87pmsoh1st.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18489&group=comp.lang.c#18489

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: How to add ssize_t a by size_t b?
Date: Fri, 01 Oct 2021 14:29:06 -0700
Organization: None to speak of
Lines: 19
Message-ID: <87pmsoh1st.fsf@nosuchdomain.example.com>
References: <c64a2f9b-4745-42f4-9bd4-ce093fbc2d68n@googlegroups.com>
<sj7h4g$19gs$1@gioia.aioe.org> <JtI5J.65761$ol1.6157@fx42.iad>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="13acd1fe4bff95bcb7ffb2395063c869";
logging-data="20569"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19mAZwsLZApCxYCPaBfmd3+"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:OcmBHMz/eX2i18bJhSM62m0+cwc=
sha1:bm59WFGIRuC1c4BnoUjoDU0CsOk=
 by: Keith Thompson - Fri, 1 Oct 2021 21:29 UTC

scott@slp53.sl.home (Scott Lurndal) writes:
> Guillaume <message@bottle.org> writes:
[...]
>>ssize_t is not standard C. It's defined in POSIX.
>>While it's a signed integer, whereas size_t is unsigned, I don't know
>>how it relates to size_t on a given implementation in terms of width.
>
> POSIX specifies that sizeof(ssize_t) == sizeof(size_t).

Does it? I couldn't find any such guarantee.

https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_types.h.html

[...]

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips
void Void(void) { Void(); } /* The recursive call of the void */

Re: How to add ssize_t a by size_t b?

<sj88eh$aub$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18495&group=comp.lang.c#18495

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: jameskuy...@alumni.caltech.edu (James Kuyper)
Newsgroups: comp.lang.c
Subject: Re: How to add ssize_t a by size_t b?
Date: Fri, 1 Oct 2021 20:17:21 -0400
Organization: A noiseless patient Spider
Lines: 23
Message-ID: <sj88eh$aub$1@dont-email.me>
References: <c64a2f9b-4745-42f4-9bd4-ce093fbc2d68n@googlegroups.com>
<sj7h4g$19gs$1@gioia.aioe.org> <JtI5J.65761$ol1.6157@fx42.iad>
<87pmsoh1st.fsf@nosuchdomain.example.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 2 Oct 2021 00:17:21 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="0043aa4b48274eece21535e86a10c636";
logging-data="11211"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+WaPnApHtIQNwJzK/HXhpDWvZqll9tZYs="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.13.0
Cancel-Lock: sha1:A4rrtZRREwx499KC/3Cw8JL1QZQ=
In-Reply-To: <87pmsoh1st.fsf@nosuchdomain.example.com>
Content-Language: en-US
 by: James Kuyper - Sat, 2 Oct 2021 00:17 UTC

On 10/1/21 5:29 PM, Keith Thompson wrote:
> scott@slp53.sl.home (Scott Lurndal) writes:
>> Guillaume <message@bottle.org> writes:
> [...]
>>> ssize_t is not standard C. It's defined in POSIX.
>>> While it's a signed integer, whereas size_t is unsigned, I don't know
>>> how it relates to size_t on a given implementation in terms of width.
>>
>> POSIX specifies that sizeof(ssize_t) == sizeof(size_t).
>
> Does it? I couldn't find any such guarantee.
>
> https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_types.h.html

In <https://pubs.opengroup.org/onlinepubs/9699919799/toc.htm> it says
the following about ssize_t:

"The wording is such that an implementation may either choose to use a
longer type or simply to use the signed version of the type that
underlies size_t."

Therefore, sizeof(ssize_t) > sizeof(size_t) is allowed. I have not yet
located "the wording" that referred to above.

Re: How to add ssize_t a by size_t b?

<87lf3cgq2u.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18497&group=comp.lang.c#18497

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: How to add ssize_t a by size_t b?
Date: Fri, 01 Oct 2021 18:42:17 -0700
Organization: None to speak of
Lines: 35
Message-ID: <87lf3cgq2u.fsf@nosuchdomain.example.com>
References: <c64a2f9b-4745-42f4-9bd4-ce093fbc2d68n@googlegroups.com>
<sj7h4g$19gs$1@gioia.aioe.org> <JtI5J.65761$ol1.6157@fx42.iad>
<87pmsoh1st.fsf@nosuchdomain.example.com> <sj88eh$aub$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="94eff854053ef23283796f3a0cb49144";
logging-data="31751"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19EKeQezuOyzt9cRxlvRfvu"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:232xYX1Fqd2i9Xwz3oTi72pOlWc=
sha1:YRz1WjARdeeVWsbmO+wYMJCUmQo=
 by: Keith Thompson - Sat, 2 Oct 2021 01:42 UTC

James Kuyper <jameskuyper@alumni.caltech.edu> writes:
> On 10/1/21 5:29 PM, Keith Thompson wrote:
>> scott@slp53.sl.home (Scott Lurndal) writes:
>>> Guillaume <message@bottle.org> writes:
>> [...]
>>>> ssize_t is not standard C. It's defined in POSIX.
>>>> While it's a signed integer, whereas size_t is unsigned, I don't know
>>>> how it relates to size_t on a given implementation in terms of width.
>>>
>>> POSIX specifies that sizeof(ssize_t) == sizeof(size_t).
>>
>> Does it? I couldn't find any such guarantee.
>>
>> https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_types.h.html
>
> In <https://pubs.opengroup.org/onlinepubs/9699919799/toc.htm> it says
> the following about ssize_t:
>
> "The wording is such that an implementation may either choose to use a
> longer type or simply to use the signed version of the type that
> underlies size_t."
>
> Therefore, sizeof(ssize_t) > sizeof(size_t) is allowed. I have not yet
> located "the wording" that referred to above.

Because of the way the site uses frames, that URL isn't meaningful. The
quote is from
https://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_xsh_chap02.html

I couldn't find "the wording" either.

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips
void Void(void) { Void(); } /* The recursive call of the void */

Re: How to add ssize_t a by size_t b?

<sja7ni$1445$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18511&group=comp.lang.c#18511

  copy link   Newsgroups: comp.lang.c
Path: rocksolid2!i2pn.org!aioe.org!Puiiztk9lHEEQC0y3uUjRA.user.46.165.242.75.POSTED!not-for-mail
From: non...@add.invalid (Manfred)
Newsgroups: comp.lang.c
Subject: Re: How to add ssize_t a by size_t b?
Date: Sat, 2 Oct 2021 20:17:22 +0200
Organization: Aioe.org NNTP Server
Message-ID: <sja7ni$1445$1@gioia.aioe.org>
References: <c64a2f9b-4745-42f4-9bd4-ce093fbc2d68n@googlegroups.com>
<sj7h4g$19gs$1@gioia.aioe.org> <JtI5J.65761$ol1.6157@fx42.iad>
<87pmsoh1st.fsf@nosuchdomain.example.com> <sj88eh$aub$1@dont-email.me>
<87lf3cgq2u.fsf@nosuchdomain.example.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="36997"; posting-host="Puiiztk9lHEEQC0y3uUjRA.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.14.0
X-Notice: Filtered by postfilter v. 0.9.2
Content-Language: en-US
 by: Manfred - Sat, 2 Oct 2021 18:17 UTC

On 10/2/2021 3:42 AM, Keith Thompson wrote:
> James Kuyper <jameskuyper@alumni.caltech.edu> writes:
>> On 10/1/21 5:29 PM, Keith Thompson wrote:
>>> scott@slp53.sl.home (Scott Lurndal) writes:
>>>> Guillaume <message@bottle.org> writes:
>>> [...]
>>>>> ssize_t is not standard C. It's defined in POSIX.
>>>>> While it's a signed integer, whereas size_t is unsigned, I don't know
>>>>> how it relates to size_t on a given implementation in terms of width.
>>>>
>>>> POSIX specifies that sizeof(ssize_t) == sizeof(size_t).
>>>
>>> Does it? I couldn't find any such guarantee.
>>>
>>> https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_types.h.html
>>
>> In <https://pubs.opengroup.org/onlinepubs/9699919799/toc.htm> it says
>> the following about ssize_t:
>>
>> "The wording is such that an implementation may either choose to use a
>> longer type or simply to use the signed version of the type that
>> underlies size_t."
>>
>> Therefore, sizeof(ssize_t) > sizeof(size_t) is allowed. I have not yet
>> located "the wording" that referred to above.
>
> Because of the way the site uses frames, that URL isn't meaningful. The
> quote is from
> https://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_xsh_chap02.html
>
> I couldn't find "the wording" either.
>

Maybe the 'wording' is "a signed analog of size_t"?

Re: How to add ssize_t a by size_t b?

<sjaaea$8oh$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18512&group=comp.lang.c#18512

  copy link   Newsgroups: comp.lang.c
Path: rocksolid2!news.neodome.net!weretis.net!feeder8.news.weretis.net!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: jameskuy...@alumni.caltech.edu (James Kuyper)
Newsgroups: comp.lang.c
Subject: Re: How to add ssize_t a by size_t b?
Date: Sat, 2 Oct 2021 15:03:36 -0400
Organization: A noiseless patient Spider
Lines: 25
Message-ID: <sjaaea$8oh$1@dont-email.me>
References: <c64a2f9b-4745-42f4-9bd4-ce093fbc2d68n@googlegroups.com>
<sj7h4g$19gs$1@gioia.aioe.org> <JtI5J.65761$ol1.6157@fx42.iad>
<87pmsoh1st.fsf@nosuchdomain.example.com> <sj88eh$aub$1@dont-email.me>
<87lf3cgq2u.fsf@nosuchdomain.example.com> <sja7ni$1445$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 2 Oct 2021 19:03:38 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="0043aa4b48274eece21535e86a10c636";
logging-data="8977"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/2kS8TPn7q5tDnpV/zI83+R5wMhXATJIU="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.13.0
Cancel-Lock: sha1:s0IJIjS3Khwe8Bl5GDQUNpwMR0s=
In-Reply-To: <sja7ni$1445$1@gioia.aioe.org>
Content-Language: en-US
 by: James Kuyper - Sat, 2 Oct 2021 19:03 UTC

On 10/2/21 2:17 PM, Manfred wrote:
> On 10/2/2021 3:42 AM, Keith Thompson wrote:
>> James Kuyper <jameskuyper@alumni.caltech.edu> writes:
....
>>> In <https://pubs.opengroup.org/onlinepubs/9699919799/toc.htm> it says
>>> the following about ssize_t:
>>>
>>> "The wording is such that an implementation may either choose to use a
>>> longer type or simply to use the signed version of the type that
>>> underlies size_t."
>>>
>>> Therefore, sizeof(ssize_t) > sizeof(size_t) is allowed. I have not yet
>>> located "the wording" that referred to above.
>>
>> Because of the way the site uses frames, that URL isn't meaningful. The
>> quote is from
>> https://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_xsh_chap02.html
>>
>> I couldn't find "the wording" either.
>>
>
> Maybe the 'wording' is "a signed analog of size_t"?

No, that's too vague to support such a detailed conclusion.

Re: How to add ssize_t a by size_t b?

<86czonrtdl.fsf@linuxsc.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18513&group=comp.lang.c#18513

  copy link   Newsgroups: comp.lang.c
Path: rocksolid2!news.neodome.net!weretis.net!feeder8.news.weretis.net!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: How to add ssize_t a by size_t b?
Date: Sat, 02 Oct 2021 14:47:50 -0700
Organization: A noiseless patient Spider
Lines: 19
Message-ID: <86czonrtdl.fsf@linuxsc.com>
References: <c64a2f9b-4745-42f4-9bd4-ce093fbc2d68n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader02.eternal-september.org; posting-host="394007ef4ff0447a8819fcdafe9c3245";
logging-data="13950"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/hJeON93KmPG2eg9bc6yr7OvDslI9LU80="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:qUjJ41WZhiA7+yLqzZs/OJ9AZoo=
sha1:67s5XWn/Tvij114cnaGEhxVBYHs=
 by: Tim Rentsch - Sat, 2 Oct 2021 21:47 UTC

wij <wyniijj@gmail.com> writes:

> To simply the question of "a+b":
>
> ssize_t add(ssize_t a, size_t b) {
> if(a+b would overflow) { set errno=ERANGE; }
> a+=b; // ?
> return a;
> }
>
> Another example:
> ssize_t a=SSIZE_T_MIN;
> size_t b=SIZE_T_MAX;
> a+=b; // Is this OK? Or, How the addition is done correctly?

This problem is doable, but it is not trivial. It makes a good
challenge problem for C experts.

Is it really important for you to find an answer?

Re: How to add ssize_t a by size_t b?

<868rzbrt5f.fsf@linuxsc.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18514&group=comp.lang.c#18514

  copy link   Newsgroups: comp.lang.c
Path: rocksolid2!news.neodome.net!news.mixmin.net!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: How to add ssize_t a by size_t b?
Date: Sat, 02 Oct 2021 14:52:44 -0700
Organization: A noiseless patient Spider
Lines: 34
Message-ID: <868rzbrt5f.fsf@linuxsc.com>
References: <c64a2f9b-4745-42f4-9bd4-ce093fbc2d68n@googlegroups.com> <87y27ch2id.fsf@nosuchdomain.example.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader02.eternal-september.org; posting-host="394007ef4ff0447a8819fcdafe9c3245";
logging-data="13950"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19zN41h2vacsxqiGNuAuxkgL5cp7uusJfM="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:JkBx5iLR04Dv5m3W9+3r4YUYQYM=
sha1:uQ/nMTrLfxfjje4C8E59FEswrKs=
 by: Tim Rentsch - Sat, 2 Oct 2021 21:52 UTC

Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:

> wij <wyniijj@gmail.com> writes:
>
>> To simply the question of "a+b":
>>
>> ssize_t add(ssize_t a, size_t b) {
>> if(a+b would overflow) { set errno=ERANGE; }
>> a+=b; // ?
>> return a;
>> }
>>
>> Another example:
>> ssize_t a=SSIZE_T_MIN;
>> size_t b=SIZE_T_MAX;
>> a+=b; // Is this OK? Or, How the addition is done correctly?
>
> In standard C, you don't, because ssize_t doesn't exist (unless you
> define it yourself).
>
> However, POSIX does define a type ssize_t, which is a signed integer
> type that can represent values up to SSIZE_MAX.
>
> The rules are given in N1570 6.3.1.8, "Usual arithmetic conversions".
>
> It's likely, but not guaranteed, that size_t and ssize_t have the same
> rank. If so, then the ssize_t value is converted to size_t, and
> assuming SSIZE_MAX <= SIZE_MAX the conversion does not change the
> value. [...]

If size_t and ssize_t have the same integer conversion rank then
SSIZE_MAX <= SIZE_MAX is guaranteed. However, converting a value
of type ssize_t to type size_t may indeed change the value,
because the value might be negative.

Re: How to add ssize_t a by size_t b?

<864k9zrs6i.fsf@linuxsc.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18516&group=comp.lang.c#18516

  copy link   Newsgroups: comp.lang.c
Path: rocksolid2!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: How to add ssize_t a by size_t b?
Date: Sat, 02 Oct 2021 15:13:41 -0700
Organization: A noiseless patient Spider
Lines: 61
Message-ID: <864k9zrs6i.fsf@linuxsc.com>
References: <c64a2f9b-4745-42f4-9bd4-ce093fbc2d68n@googlegroups.com> <sj7h4g$19gs$1@gioia.aioe.org> <sj7ler$sk8$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader02.eternal-september.org; posting-host="1a64f0de92465f6d033f50ce67cc535c";
logging-data="26763"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19UflWYFI5h8XAjF+B/I++n/eJVaYrk3ig="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:HB5MI/0QOzGRFlXtdJultFX/A/Q=
sha1:00GmiS2cDoAAhwH0BUnRdCfTA2E=
 by: Tim Rentsch - Sat, 2 Oct 2021 22:13 UTC

James Kuyper <jameskuyper@alumni.caltech.edu> writes:

> On 10/1/21 1:39 PM, Guillaume wrote:
>
>> Le 01/10/2021 at 17:32, wij a ecrit:
>>
>>> To simply the question of "a+b":
>>>
>>> ssize_t add(ssize_t a, size_t b) {
>>> if(a+b would overflow) { set errno=ERANGE; }
>>> a+=b; // ?
>>> return a;
>>> }
>>>
>>> Another example:
>>> ssize_t a=SSIZE_T_MIN;
>>> size_t b=SIZE_T_MAX;
>>> a+=b; // Is this OK? Or, How the addition is done correctly?
>>
>> ssize_t is not standard C. It's defined in POSIX.
>> While it's a signed integer, whereas size_t is unsigned, I don't know
>> how it relates to size_t on a given implementation in terms of width.
>
> ssize_t is a signed integer type with the same width as size_t.

I don't see anything on the pubs.opengroup.org website that
requires that.

> Therefore, they should have the same integer conversion rank. Since
> the sign bit is included in the width, SSIZE_MAX is guaranteed to be
> smaller than SIZE_MAX.
>
> The usual arithmetic conversions apply (6.5.6p5). The ssize_t value
> is first converted to size_t (6.3.1.8p1). That is a well-defined
> conversion - negative ssize_t values have SIZE_MAX+1 added to them
> (6.3.1.3p2). The sum is calculated using size_t math, which will
> produce what I assume is the desired result even if a is negative,
> so long as it is smaller than b.
>
> Upon assignment, the result is converted to ssize_t. Values greater
> than SSIZE_MAX would not result in undefined behavior, as you
> suggested. Instead, they would result in an implementation-defined
> value or the raising of an implementation-defined signal (6.3.1.3p3).
> This code will probably not work as desired on an implementation that
> chooses to raise a signal, and the implementation- defined value is
> not guaranteed to be the one that he wants. Therefore, setting errno
> would not be sufficient, the final conversion must be actively
> prevented from occurring if it would otherwise overflow.
>
> I would write the function as:
>
> size_t c = a + b;
> if(c > SSIZE_MAX)
> return overflow_value;
> else
> return c;
>
> where overflow_value is whatever value he wants add() to return
> when there's an overflow. [...]

An exemplary proposal: short, simple, and wrong.

Re: How to add ssize_t a by size_t b?

<86mtnmquno.fsf@linuxsc.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18584&group=comp.lang.c#18584

  copy link   Newsgroups: comp.lang.c
Path: rocksolid2!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: How to add ssize_t a by size_t b?
Date: Wed, 06 Oct 2021 04:06:51 -0700
Organization: A noiseless patient Spider
Lines: 28
Message-ID: <86mtnmquno.fsf@linuxsc.com>
References: <c64a2f9b-4745-42f4-9bd4-ce093fbc2d68n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader02.eternal-september.org; posting-host="f63a7b6827eb8960d7c84af554cfeab9";
logging-data="28167"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+HF8dxTljm+54zTOkCVnicRdvGx3dpty8="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:1xjIZP+yktRAXlOzfS0ICdO3MOI=
sha1:mbNFr9zUS4nhJYdN3YkyCzdnmbs=
 by: Tim Rentsch - Wed, 6 Oct 2021 11:06 UTC

wij <wyniijj@gmail.com> writes:

> To simply the question of "a+b":
>
> ssize_t add(ssize_t a, size_t b) {
> if(a+b would overflow) { set errno=ERANGE; }
> a+=b; // ?
> return a;
> }
>
> Another example:
> ssize_t a=SSIZE_T_MIN;
> size_t b=SIZE_T_MAX;
> a+=b; // Is this OK? Or, How the addition is done correctly?

For those who may be interested, here is a function to compute
the sum when the result is within the range of ssize_t, and
give an error indication when it isn't.

ssize_t
add_ssize_and_size( ssize_t const a, size_t const b ){
if( a >= 0 && SSIZE_MAX - a >= b ) return a + b;
if( a < 0 && b <= SSIZE_MAX ) return a + (ssize_t){ b };
if( a < 0 && b-1-SSIZE_MAX <= -(a+1) ) return b-1 - -(a+1);

return errno = ERANGE, SSIZE_MAX; // or some other suitable value
}

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor