Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #162948
| From | Tim Rentsch <tr.17687@z991.linuxsc.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: How to add ssize_t a by size_t b? |
| Date | 2021-10-02 15:13 -0700 |
| Organization | A noiseless patient Spider |
| Message-ID | <864k9zrs6i.fsf@linuxsc.com> (permalink) |
| References | <c64a2f9b-4745-42f4-9bd4-ce093fbc2d68n@googlegroups.com> <sj7h4g$19gs$1@gioia.aioe.org> <sj7ler$sk8$1@dont-email.me> |
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.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
How to add ssize_t a by size_t b? wij <wyniijj@gmail.com> - 2021-10-01 08:32 -0700
Re: How to add ssize_t a by size_t b? Guillaume <message@bottle.org> - 2021-10-01 19:39 +0200
Re: How to add ssize_t a by size_t b? scott@slp53.sl.home (Scott Lurndal) - 2021-10-01 18:31 +0000
Re: How to add ssize_t a by size_t b? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-10-01 14:29 -0700
Re: How to add ssize_t a by size_t b? James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-10-01 20:17 -0400
Re: How to add ssize_t a by size_t b? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-10-01 18:42 -0700
Re: How to add ssize_t a by size_t b? Manfred <noname@add.invalid> - 2021-10-02 20:17 +0200
Re: How to add ssize_t a by size_t b? James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-10-02 15:03 -0400
Re: How to add ssize_t a by size_t b? James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-10-01 14:53 -0400
Re: How to add ssize_t a by size_t b? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-10-02 15:13 -0700
Re: How to add ssize_t a by size_t b? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-10-01 14:13 -0700
Re: How to add ssize_t a by size_t b? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-10-02 14:52 -0700
Re: How to add ssize_t a by size_t b? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-10-02 14:47 -0700
Re: How to add ssize_t a by size_t b? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-10-06 04:06 -0700
csiph-web