Path: csiph.com!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail From: 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: 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= Xref: csiph.com comp.lang.c:162945 wij 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?