llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.25k stars 12.08k forks source link

Clang static analyzer - `Past-the-end iterator dereferenced` in ranged based for loop #89025

Open tgurriet opened 7 months ago

tgurriet commented 7 months ago

Hello,

I'm using the latest clang-18, and the static-analyzer/clang-tidy through CodeChecker.

I have a little issue with it flagging all my ranged based for loop with Past-the-end iterator dereferenced.

For example, in the following function

  void log(const log_tags_t & tags, const LogLevel lvl, const std::string & str)
  {
    for (const auto & logger : m_loggers) { logger(tags, lvl, str); }
  }

The for (const auto & logger : m_loggers) is being flagged. I can't think of any context in which a range based for loop would do such a thing.

Any idea?

Thanks in advance

llvmbot commented 7 months ago

@llvm/issue-subscribers-clang-static-analyzer

Author: Thomas Gurriet (tgurriet)

Hello, I'm using the latest clang-18, and the static-analyzer/clang-tidy through CodeChecker. I have a little issue with it flagging all my ranged based for loop with `Past-the-end iterator dereferenced`. For example, in the following function ``` void log(const log_tags_t & tags, const LogLevel lvl, const std::string & str) { for (const auto & logger : m_loggers) { logger(tags, lvl, str); } } ``` The ` for (const auto & logger : m_loggers)` is being flagged. I can't think of any context in which a range based for loop would do such a thing. Any idea? Thanks in advance
steakhal commented 7 months ago

Do you have a self-contained example that we could feed into clang to see the mentioned behavior?

steakhal commented 7 months ago

I couldn't reproduce this issue. https://godbolt.org/z/7bY18caWx Let me know if your flags are different or the code looks differently.

tgurriet commented 7 months ago

@steakhal It's part of a large project. I'll try to extract a subset that highlights the behavior.

Isn't it strange though? I don't understand much about how the analyser is implemented, but it feels like such warning shouldn't be triggerable, no matter what's triggering it !?

steakhal commented 7 months ago

@steakhal It's part of a large project. I'll try to extract a subset that highlights the behavior.

Let me know if you need some help. If the project is open-source, you could just upload a preprocessed version of it, and we wil ltake it from there. Use the -E commandline flag for it.

If it's a closed-source, and you want it to be fixed, you have two options:

Isn't it strange though? I don't understand much about how the analyser is implemented, but it feels like such warning shouldn't be triggerable, no matter what's triggering it !?

This is just a bug, like many others. I don't see how it would be any special.

steakhal commented 6 months ago

I guess, this remains unactionable.

tgurriet commented 6 months ago

Yes, sorry, I've been pretty busy. I'll get back to this issue asap.

steakhal commented 6 months ago

Yes, sorry, I've been pretty busy. I'll get back to this issue asap.

I see. No worries. Take your time. I couldn't look into this now anyways :D

mboyko2000 commented 1 month ago

This will reproduce the issue. Notice the warning is only produced on C++20 and 23 dialects. C++17 does not produce the warning. https://godbolt.org/z/vnc9zTfrd

tgurriet commented 1 month ago

This will reproduce the issue. Notice the warning is only produced on C++20 and 23 dialects. C++17 does not produce the warning. https://godbolt.org/z/vnc9zTfrd

Thanks for pushing this forward!