public inbox for liba2i@lists.linux.dev
 help / color / mirror / Atom feed
From: Alejandro Colomar <alx@kernel.org>
To: daniel.marjamaki@gmail.com
Cc: liba2i@lists.linux.dev
Subject: cppcheck(1) internalAstError
Date: Sun, 5 May 2024 18:28:06 +0200	[thread overview]
Message-ID: <Zjezlvnu-cMIg2SF@debian> (raw)

[-- Attachment #1: Type: text/plain, Size: 6859 bytes --]

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=print
	CPPCHECK	.tmp/a2i/strtoi.c.lint-c.cppcheck.touch
	cppcheck --enable=all --error-exitcode=2 --inconclusive --check-level=exhaustive --quiet -D__GNUC__ -D__STDC_VERSION__=202000L --suppressions-list=./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) == 0 && *status == ERANGE)
	    ^
	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 <alx@kernel.org>
// 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_ = s;                                                        \
	char  **endp_ = endp;                                                 \
	int   base_ = base;                                                   \
	TYPE  min_ = min;                                                     \
	TYPE  max_ = max;                                                     \
	int   *status_ = status;                                              \
                                                                              \
	int   errno_saved_, st_;                                              \
	char  *e_;                                                            \
	TYPE  n_;                                                             \
                                                                              \
	if (endp_ == NULL)                                                    \
		endp_ = &e_;                                                  \
	if (status_ == NULL)                                                  \
		status_ = &st_;                                               \
                                                                              \
	if (base != 0 && (base_ < 2 || base_ > 36)) {                         \
		*status_ = EINVAL;                                            \
		n_ = 0;                                                       \
                                                                              \
	} else {                                                              \
		errno_saved_ = errno;                                         \
		errno = 0;                                                    \
		n_ = _Generic((TYPE) 0,                                       \
			intmax_t:  strtoimax,                                 \
			uintmax_t: strtoumax                                  \
		)(s_, endp_, base_);                                          \
                                                                              \
		if (*endp_ == s_)                                             \
			*status_ = ECANCELED;                                 \
		else if (errno == ERANGE || n_ < min_ || n_ > max_)           \
			*status_ = ERANGE;                                    \
		else if (**endp_ != '\0')                                     \
			*status_ = ENOTSUP;                                   \
		else                                                          \
			*status_ = 0;                                         \
                                                                              \
		errno = 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 == NULL)
		status = &st;
	if (a2i_strtoi(s, endp, base, 0, 1, status) == 0 && *status == ERANGE)
		return min;

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




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

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

                 reply	other threads:[~2024-05-05 16:28 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Zjezlvnu-cMIg2SF@debian \
    --to=alx@kernel.org \
    --cc=daniel.marjamaki@gmail.com \
    --cc=liba2i@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox