cppcheck(1) internalAstError

Alejandro Colomar <[email protected]> Sun, 5 May 2024 18:28:06 +0200
Newsgroups dev.linux.lists.liba2i
Message-ID <Zjezlvnu-cMIg2SF@debian>
--c/aT7tCNVvw5tLz1
Content-Type: text/plain; protected-headers=v1; charset=utf-8
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable
Date: Sun, 5 May 2024 18:28:06 +0200
From: Alejandro Colomar <[email protected]>
To: [email protected]
Cc: [email protected]
Subject: cppcheck(1) internalAstError

Hi Daniel,

I'm reporting a bug in cppcheck(1).  Since I find the method to report
bugs a bit cumbersome (requesting an account, ...), I'm sending it to
the mailing list of the project where I found the bug.  Feel free to
open an account for me at your place if you find it appropriate, and
also feel free to forward this bug report there.

I'm using the cppckech(1) that ships with Debian Sid:

	$ cppcheck --version
	Cppcheck 2.14.0

And found a new error in my code after recent changes, which otherwise
seem to be fine, since it builds, and all the tests pass.

	$ make lint-c-cppcheck --debug=3Dprint
	CPPCHECK	.tmp/a2i/strtoi.c.lint-c.cppcheck.touch
	cppcheck --enable=3Dall --error-exitcode=3D2 --inconclusive --check-level=
=3Dexhaustive --quiet -D__GNUC__ -D__STDC_VERSION__=3D202000L --suppression=
s-list=3D./etc/cppcheck/cppcheck.suppress  -I ./include lib/src/a2i/strtoi.c
	lib/src/a2i/strtoi.c:120:5: error: Syntax Error: AST broken, 'if' doesn't =
have two operands. [internalAstError]
	 if (a2i_strtoi(s, endp, base, 0, 1, status) =3D=3D 0 && *status =3D=3D ER=
ANGE)
	    ^
	make: *** [/home/alx/src/alx/liba2i/main/share/mk/lint/c/cppcheck.mk:39: .=
tmp/a2i/strtoi.c.lint-c.cppcheck.touch] Error 2

The file is below, although you may need the entire project (for the
includes, ...).  Feel free to try the repo:

<https://git.kernel.org/pub/scm/libs/liba2i/liba2i.git/>

I'm having this problem at commit
f7e09a7d41cd ("etc/cpplint/cpplint.cfg: Add -readability/casting").

Have a lovely day!
Alex

---

alx@debian:~/src/alx/liba2i/main$ cat lib/src/a2i/strtoi.c
// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar <[email protected]>
// SPDX-License-Identifier: LGPL-3.0-only WITH LGPL-3.0-linking-exception


#include <a2i/strtoi.h>

#include <errno.h>
#include <inttypes.h>
#include <stddef.h>
#include <stdint.h>
#include <sys/param.h>

#include <a2i/qual.h>


#define a2i_strtoI(TYPE, s, endp, base, min, max, status)                  =
   \
({                                                                         =
   \
	char  *s_ =3D s;                                                        \
	char  **endp_ =3D endp;                                                 \
	int   base_ =3D base;                                                   \
	TYPE  min_ =3D min;                                                     \
	TYPE  max_ =3D max;                                                     \
	int   *status_ =3D status;                                              \
                                                                           =
   \
	int   errno_saved_, st_;                                              \
	char  *e_;                                                            \
	TYPE  n_;                                                             \
                                                                           =
   \
	if (endp_ =3D=3D NULL)                                                    \
		endp_ =3D &e_;                                                  \
	if (status_ =3D=3D NULL)                                                  \
		status_ =3D &st_;                                               \
                                                                           =
   \
	if (base !=3D 0 && (base_ < 2 || base_ > 36)) {                         \
		*status_ =3D EINVAL;                                            \
		n_ =3D 0;                                                       \
                                                                           =
   \
	} else {                                                              \
		errno_saved_ =3D errno;                                         \
		errno =3D 0;                                                    \
		n_ =3D _Generic((TYPE) 0,                                       \
			intmax_t:  strtoimax,                                 \
			uintmax_t: strtoumax                                  \
		)(s_, endp_, base_);                                          \
                                                                           =
   \
		if (*endp_ =3D=3D s_)                                             \
			*status_ =3D ECANCELED;                                 \
		else if (errno =3D=3D ERANGE || n_ < min_ || n_ > max_)           \
			*status_ =3D ERANGE;                                    \
		else if (**endp_ !=3D '\0')                                     \
			*status_ =3D ENOTSUP;                                   \
		else                                                          \
			*status_ =3D 0;                                         \
                                                                           =
   \
		errno =3D errno_saved_;                                         \
	}                                                                     \
	MAX(min_, MIN(max_, n_));                                             \
})


#if defined(__clang__)
# pragma clang assume_nonnull begin
#endif
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-qual"
intmax_t
a2i_strtoi_c(const char *s,
    const char **a2i_nullable restrict endp, int base,
    intmax_t min, intmax_t max, int *a2i_nullable restrict status)
{
	return a2i_strtoi((char *) s, (char **a2i_nullable) endp, base,
	                  min, max, status);
}

uintmax_t
a2i_strtou_c(const char *s,
    const char **a2i_nullable restrict endp, int base,
    uintmax_t min, uintmax_t max, int *a2i_nullable restrict status)
{
	return a2i_strtou((char *) s, (char **a2i_nullable) endp, base,
	                  min, max, status);
}

uintmax_t
a2i_strtou_noneg_c(const char *s,
    const char **a2i_nullable restrict endp, int base,
    uintmax_t min, uintmax_t max, int *a2i_nullable restrict status)
{
	return a2i_strtou_noneg((char *) s, (char **a2i_nullable) endp, base,
	                        min, max, status);
}
#pragma GCC diagnostic pop


intmax_t
(a2i_strtoi)(char *s,
    char **a2i_nullable restrict endp, int base,
    intmax_t min, intmax_t max, int *a2i_nullable restrict status)
{
	return a2i_strtoI(intmax_t, s, endp, base, min, max, status);
}

uintmax_t
(a2i_strtou)(char *s,
    char **a2i_nullable restrict endp, int base,
    uintmax_t min, uintmax_t max, int *a2i_nullable restrict status)
{
	return a2i_strtoI(uintmax_t, s, endp, base, min, max, status);
}

uintmax_t
(a2i_strtou_noneg)(char *s,
    char **a2i_nullable restrict endp, int base,
    uintmax_t min, uintmax_t max, int *a2i_nullable restrict status)
{
	int  st;

	if (status =3D=3D NULL)
		status =3D &st;
	if (a2i_strtoi(s, endp, base, 0, 1, status) =3D=3D 0 && *status =3D=3D ERA=
NGE)
		return min;

	return a2i_strtou(s, endp, base, min, max, status);
}
#if defined(__clang__)
# pragma clang assume_nonnull end
#endif




--=20
<https://www.alejandro-colomar.es/>

--c/aT7tCNVvw5tLz1
Content-Type: application/pgp-signature; name="signature.asc"

-----BEGIN PGP SIGNATURE-----

iQIzBAABCgAdFiEE6jqH8KTroDDkXfJAnowa+77/2zIFAmY3s5YACgkQnowa+77/
2zIOPQ/+M3BnVfVzZzwFAc0CYDyGXLfyC1kRuxCZ7crrjF4oOWaSoWIQjAmZsXWU
uNGCa3UDvxlno4fRDLOGsI9SWy/pSorO7y22uZlq7dorFiHqmAir7jhpcMvGnYl3
U7l81eGxNvcxlPO6cezutmDZYH1H/g75To921KjXiV7FoKM9ROtYklfCkVz1RcJN
f8MfIQaBZFCtssgtKLQBOsySirI4LkKINW1Hxe016+lEPlaVuOCXZbstaHIFmyiI
Ebcj+Ppq/l38kTLtVDKgzHDXFC/nXYDGRrPUuT0tbectSsaBqU5JMk3M6Yq90AJP
lZD0gAHFslPjfoMMnKT4vxbAsJ6SCLM4wP+I93wCVVpJMTumZEvHb1K4ZmASl0Cn
MrHXkdAFo/OqQAP112VR3bWGElNziM3pCfcSau2Q+LxI0SVQyBNbTJAyeD/DTYOW
YLtrl9AOPdtz6+yRud3LkiRhvpdjciLsUHMGXYk0PFKgRDRp2SP3bL/7psFcPNkL
NvYMN7gD9IuAUI5iQCTbLouR3+VXTUnuYWpAC5aBy39VB481J343Ekwa+/WjdCrP
dRHGSU7RwuY6qu347TR9onW2wfdtb+48iX/uXBWKIQUp90zx7cRS0EuvNnCxXA+K
y6Ww7lcFRcRJJUrrBVwXvbWG/K+9FuHneV/B3ABWLLbO3rIs7OM=
=P5xj
-----END PGP SIGNATURE-----

--c/aT7tCNVvw5tLz1--