Fix UB in JpegCompressionDistortion: use data() for past-the-end pointer#6251
Fix UB in JpegCompressionDistortion: use data() for past-the-end pointer#6251JanuszL wants to merge 3 commits intoNVIDIA:mainfrom
Conversation
…ter. - Replace `&shape[ndim]` and `&shape[0]` with `shape.data() + ndim` and `shape.data()` in JpegCompressionDistortionCPU::RunImpl. - `&shape[ndim]` calls operator[] with an out-of-bounds index before taking the address, which is undefined behavior and triggers assertion failures in debug builds. Using `shape.data() + ndim` computes the past-the-end pointer via pointer arithmetic, which is explicitly permitted by the C++ standard ([expr.add]) and is the correct way to form a one-past-the-end pointer without dereferencing it. Signed-off-by: Janusz Lisiecki <jlisiecki@nvidia.com>
|
!build |
Greptile SummaryThis PR fixes undefined behavior in
The comment explaining the empty-range behavior when Confidence Score: 5/5
Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[RunImpl called] --> B[Get tensor shape span]
B --> C{f_dim == -1?}
C -- Yes --> D["shape.begin() + f_dim+1 == shape.begin()\n→ empty range → nframes = 1"]
C -- No --> E["shape.begin() + f_dim+1\n→ valid iterator → nframes = F dimension volume"]
D --> F["frame_size = volume(shape.begin() + f_dim+1, shape.begin() + ndim)"]
E --> F
F --> G[Dispatch work per frame to thread pool]
G --> H[RunJpegDistortionCPU per frame]
Last reviewed commit: 2c168f5 |
|
CI MESSAGE: [45811839]: BUILD STARTED |
|
CI MESSAGE: [45811839]: BUILD PASSED |
| volume(shape.data(), shape.data() + f_dim + 1); // note that if f_dim is -1, this | ||
| // evaluates to an empty range, volume of 1 | ||
| int64_t frame_size = volume(shape.data() + f_dim + 1, shape.data() + ndim); |
There was a problem hiding this comment.
I think that formally it's better to do:
| volume(shape.data(), shape.data() + f_dim + 1); // note that if f_dim is -1, this | |
| // evaluates to an empty range, volume of 1 | |
| int64_t frame_size = volume(shape.data() + f_dim + 1, shape.data() + ndim); | |
| volume(shape.begin(), shape.begin() + f_dim + 1); // note that if f_dim is -1, this | |
| // evaluates to an empty range, volume of 1 | |
| int64_t frame_size = volume(shape.begin() + f_dim + 1, shape.begin() + ndim); |
In case of some super-debug builds, advancing an iterator may be checked, whereas a raw pointer (as returned by data()) won't be.
Also, note that the out of bound will never occur in practice, as the only accepted layouts are HWC and FHWC (see line 36), so this is not an "important fix" in that it doesn't create an exploitable attack surface.
There was a problem hiding this comment.
Fixed. You are right regarding severity.
Signed-off-by: Janusz Lisiecki <jlisiecki@nvidia.com>
|
!build |
|
CI MESSAGE: [45872745]: BUILD STARTED |
|
CI MESSAGE: [45872745]: BUILD FAILED |
&shape[ndim]and&shape[0]withshape.data() + ndimandshape.data()in JpegCompressionDistortionCPU::RunImpl.&shape[ndim]calls operator[] with an out-of-bounds index before takingthe address, which is undefined behavior and triggers assertion failures in
debug builds. Using
shape.data() + ndimcomputes the past-the-end pointervia pointer arithmetic, which is explicitly permitted by the C++ standard
([expr.add]) and is the correct way to form a one-past-the-end pointer
without dereferencing it.
Category:
Bug fix (non-breaking change which fixes an issue)
Description:
&shape[ndim]and&shape[0]withshape.data() + ndimandshape.data()in JpegCompressionDistortionCPU::RunImpl.&shape[ndim]calls operator[] with an out-of-bounds index before takingthe address, which is undefined behavior and triggers assertion failures in
debug builds. Using
shape.data() + ndimcomputes the past-the-end pointervia pointer arithmetic, which is explicitly permitted by the C++ standard
([expr.add]) and is the correct way to form a one-past-the-end pointer
without dereferencing it.
Additional information:
Affected modules and functionalities:
Key points relevant for the review:
Tests:
Checklist
Documentation
DALI team only
Requirements
REQ IDs: N/A
JIRA TASK: N/A