#1076061 cppcheck: Syntax error on func(&data)[0] valid C code

Package:
cppcheck
Source:
cppcheck
Description:
tool for static C/C++ code analysis (CLI)
Submitter:
Guillem Jover
Date:
2026-06-29 20:33:03 UTC
Severity:
normal
Tags:
#1076061#5
Date:
2024-07-10 01:14:15 UTC
From:
To:
Hi!

I just stumbled over the following issue after some code changes in
dpkg, which is currently failing one of its CI (the one in salsa)
due to this, but I'll modify the involved code to avoid this though.

It seems cppcheck does not handle correctly dereferencing a pointer
returned by a function, when said function gets passed a pointer by
reference.

Here's the smallest reproducer I could concoct:

  ,--- deref-return.c
  const char *func(int *data) {
          return "some string" + *data;
  }
  int main() {
          int data = 2;
          int *pdata = &data;
          if (func(&data)[0] == 'm')
                  return 0;
          return 1;
  }
  `---

  ,--- shell
  $ cppcheck deref-return.c
  Checking deref-return.c ...
  deref-return.c:7:12: error: Code '(& data' is invalid C code. Use --std or --language to configure the language. [syntaxError]
   if (func(&data)[0] == 'm')
             ^
  `---

If I change the call to «ref(pdata)[0]», then cppcheck is happy with
that.

Thanks,
Guillem

#1076061#14
Date:
2026-06-29 20:30:32 UTC
From:
To:
This has been fixed upstream quite a while ago but somehow slipped through the
cracks.

Best regards,
   Joachim