Re: [PATCH v3] diff: check range before dereferencing an array element
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Apr 29, 2025, 19:39 UTC
- Message-ID
- <xmqqcycu69ky.fsf@gitster.g>
- In-Reply-To
- <pull.1887.v3.git.1745926679028.gitgitgadget@gmail.com>
"Johannes Schindelin via GitGitGadget" <gitgitgadget@gmail.com> writes:
Show 16 quoted lines
> From: Johannes Schindelin <johannes.schindelin@gmx.de> > > Before accessing an array element at a given index, it should be > verified that the index is within the desired bounds, not afterwards, > otherwise it may not make sense to even access the array element in the > first place. This is the point of CodeQL's > `cpp/offset-use-before-range-check` rule. > > This CodeQL rule unfortunately is also triggered by the > `fill_es_indent_data()` code, even though the condition `off < len - 1` > does not even need to guarantee that the offset is in bounds (`s` points > to a NUL-terminated string, for which `s[off] == '\r'` would fail before > running out of bounds). > > Let's work around this rare false positive to help us use an otherwise > mostly useful tool is a worthy thing to do.
Thanks. I have almost forgotten about this one. The above explanation works very well.