[U-Boot] [PATCH] lib/crc16: use non-C99 loop style

Philipp Tomsich philipp.tomsich at theobroma-systems.com
Wed Feb 13 22:50:33 UTC 2019



> On 13.02.2019, at 22:57, Thomas Petazzoni <thomas.petazzoni at bootlin.com> wrote:
> 
> Commit 51c2345bd24837f9f67f16268da6dc71573f1325 ("Roll CRC16-CCITT
> into the hash infrastructure") has modified the crc16 code by adding a
> C99-style loop where the loop iterator is declared inside the for()
> statement. This breaks the build with old compiler such as gcc 4.7,
> that do not default to C99:

I thought U-Boot now has a requirement of GCC6 or newer (i.e. C99 being the
default C dialect).  While there’s still a few distributions out there that use legacy
compiler versions, we might want to consider having the same requirement for
the host tools.

> ./tools/../lib/crc16.c: In function 'crc16_ccitt':
> ./tools/../lib/crc16.c:70:2: error: 'for' loop initial declarations are only allowed in C99 mode
> ./tools/../lib/crc16.c:70:2: note: use option -std=c99 or -std=gnu99 to compile your code
> 
> Switching to the regular coding style used in the rest of U-Boot
> allows to fix this build issue.

At this point in time (although Tom will have the final word), I would strongly
encourage us adopting C99 in our coding style.

C99 has been around for  a while now (it will be 20 years this year that the
standard came out) and we already have seen ISO/IEC 9899:2011 and
ISO/IEC 9899:2018 published.  Note that these also improvements for
system-level development (stdatomics.h and stdalign.h are underutilised
extensions for that purpose)...

> Signed-off-by: Thomas Petazzoni <thomas.petazzoni at bootlin.com>
> ---
> lib/crc16.c | 4 +++-
> 1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/lib/crc16.c b/lib/crc16.c
> index f46ba727c9..89d2cff131 100644
> --- a/lib/crc16.c
> +++ b/lib/crc16.c
> @@ -67,7 +67,9 @@ static const uint16_t crc16_tab[] = {
> 
> uint16_t crc16_ccitt(uint16_t cksum, const unsigned char *buf, int len)

If we are trying to turn back the wheel of time, then even the const modifier
would be up for discussion: after all, this was a GNU extension in gnu89…

> {
> -	for (int i = 0;  i < len;  i++)
> +	int i;
> +
> +	for (i = 0;  i < len;  i++)
> 		cksum = crc16_tab[((cksum>>8) ^ *buf++) & 0xff] ^ (cksum << 8);
> 
> 	return cksum;
> -- 
> 2.20.1
> 



More information about the U-Boot mailing list