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 avoid an overflow during multiplication? Date: Fri, 21 Jan 2022 23:37:07 -0800 Organization: A noiseless patient Spider Lines: 69 Message-ID: <8635lgp6fw.fsf@linuxsc.com> References: <8735m954yz.fsf@bsb.me.uk> <877dbk36qu.fsf@nosuchdomain.example.com> <86v8ydp57h.fsf@linuxsc.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="434625eb2bdf5b21805d62a567edd212"; logging-data="20807"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+SKMCgM0FU6ccg/CV7P7Im4Rnjc+36pcE=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:p3wMiUAf1v5bk00AhuRXWb7zpvE= sha1:ICLGgFA8mXJOTffeHqefJ5LVSlU= Xref: csiph.com comp.lang.c:164532 Mateusz Viste writes: (Again I am responding in several parts to help keep the various aspects separate.) > 2022-01-21 at 05:51 -0800, Tim Rentsch wrote: > >> Also, I'm curious to know what extensions, if any, from the gnu >> additions you make use of. > > [...] Here is [what was found]: > > __uint128_t (also uint64_t / uint32_t / uint8_t but it seems gcc > tolerates those even in c89 mode, as long as stdint.h is included) > struct timeval > struct timespec > DT_DIR / DT_REG > PATH_MAX > > clock_gettime() / CLOCK_MONOTONIC > select() / fd_set / FD_SET() / FD_ZERO() / FD_ISSET() > strcasecmp() > getaddrinfo() / freeaddrinfo() / struct addrinfo / gai_strerror() > inet_aton() > mrand48() > snprintf() / vsnprintf() > strdup() > realpath() > chroot() > fileno() > popen() / pclose() > setenv() / unsetenv() > vsyslog() I think almost all of these can be made available, and fairly easily, without having to resort to -std=gnuXX or general use of any #defines of _XOPEN_SOURCE or similar symbols. My usual practice is to put POSIX-related interfaces in a separate file (eg, posix.h and posix.c), with only posix.c turning on any needed extra functionality by using #define _XOPEN_SOURCE or whatever. Most of the interfaces listed above can be made available directly through a posix.h header and used in standard-conforming C code. I had no trouble making such a header for things like strdup, chroot, setenv, and almost all the rest. The macros for numeric constants cannot be supplied directly but it isn't difficult to work around that aspect in various ways. The various struct types can be used but only in pointer form rather than directly. It's easy to work around that, if perhaps somewhat tedious, by providing an abstract interface for those types in posix.h (and posix.c implementing it). The one problem child is select() and friends. My suggestion there is to make a slightly higher level interface and used that rather than using select() directly. The motivation for doing all this is to get a program that is almost exclusively purely standard conforming. The big problem with using magic #defines or -std=gnuXX is like the proverbial saying about a box of chocolates: you never know what you're going to get. Worse, it can and sometimes does change between different compiler releases. Localizing all that stuff to a single .c file reduces the surface area of exposure and also makes it easy to identify what non-standard interfaces are being used and where.