Skip to content

Fix crash from out-of-bounds copy in WaveClipSpectrumCache::GetSpectrogram#8974

Closed
haileys wants to merge 2 commits intoaudacity:audacity3from
haileys:audacity3+fix-spectrogram-crash
Closed

Fix crash from out-of-bounds copy in WaveClipSpectrumCache::GetSpectrogram#8974
haileys wants to merge 2 commits intoaudacity:audacity3from
haileys:audacity3+fix-spectrogram-crash

Conversation

@haileys
Copy link
Copy Markdown

@haileys haileys commented Jun 26, 2025

Resolves: #8851

This fixes a crash bug in WaveClipSpectrumCache::GetSpectrogram. I've cleaned up the arithmetic for calculating the begin and end indices for copying cached spectrogram data. Previously copyBegin + oldX0 could exceed mSpecCache->len after shrinking mSpecCache, and this is where the crash on out of bounds access was coming from.

I've also renamed SpecCache::Grow to SpecCache::Resize to more accurately reflect what this function does, while also moving its invocation to after the crashing memmove to guarantee that we are never indexing out of bounds.

  • I signed CLA
  • The title of the pull request describes an issue it addresses
  • If changes are extensive, then there is a sequence of easily reviewable commits
  • Each commit's message describes its purpose and effects
  • There are no behavior changes unnecessary for the stated purpose of the PR

Recommended:

  • Each commit compiles and runs on my machine without known undesirable changes of behavior

@haileys haileys force-pushed the audacity3+fix-spectrogram-crash branch from c849c75 to bdc4ae2 Compare June 26, 2025 06:53
@haileys haileys force-pushed the audacity3+fix-spectrogram-crash branch from bdc4ae2 to 21ee6d3 Compare June 26, 2025 08:33
@kryksyh kryksyh requested a review from Copilot June 26, 2025 14:37
@kryksyh kryksyh self-assigned this Jun 26, 2025
@kryksyh kryksyh linked an issue Jun 26, 2025 that may be closed by this pull request
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a crash in WaveClipSpectrumCache::GetSpectrogram by tightening the range calculations for copying cached data, renaming Grow to Resize, and moving the resize call to avoid out-of-bounds access.

  • Replaced SpecCache::Grow with Resize across UI, header, and implementation.
  • Reworked copyBegin/copyEnd logic to calculate a safe copy range.
  • Deferred the cache resize until after data is copied to preserve valid contents.

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/tracks/playabletrack/wavetrack/ui/SpectrumView.cpp Updated call from specCache.Grow to specCache.Resize to match renamed method.
src/tracks/playabletrack/wavetrack/ui/SpectrumCache.h Renamed Grow to Resize in declaration and adjusted comment to reflect new behavior.
src/tracks/playabletrack/wavetrack/ui/SpectrumCache.cpp Refactored out-of-bounds copy logic, moved Resize after memmove, and updated method name.
Comments suppressed due to low confidence (1)

src/tracks/playabletrack/wavetrack/ui/SpectrumCache.cpp:535

  • [nitpick] The comment indicates contents remain unchanged, but when shrinking the cache the data outside the new size range will be discarded. Clarify that only overlapping data is preserved.
   // Resize the cache, keep the contents unchanged.

));
int copyLen = std::max(0, int(mSpecCache->len) - std::abs(oldX0));
copyBegin = oldX0 >= 0 ? 0 : std::abs(oldX0);
copyEnd = copyBegin + copyLen;
Copy link

Copilot AI Jun 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

copyBegin and copyEnd are not clamped to numPixels, which could lead to copying beyond buffer if the number of destination pixels is smaller than the cache length. Consider clamping copyBegin and copyEnd to [0, numPixels].

Suggested change
copyEnd = copyBegin + copyLen;
copyEnd = copyBegin + copyLen;
copyBegin = std::clamp(copyBegin, 0, numPixels);
copyEnd = std::clamp(copyEnd, 0, numPixels);

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This AI slop is wrong, copying beyond the end of the buffer is exactly what this PR fixes.

void Grow(
// Resize the cache, while preserving the (possibly now invalid!) contents if growing
void Resize(
size_t len_, SpectrogramSettings& settings, double samplesPerPixel,
Copy link

Copilot AI Jun 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Parameter 'settings' is passed by non-const reference, but the method does not modify it. Consider changing it to 'const SpectrogramSettings&' for const-correctness.

Suggested change
size_t len_, SpectrogramSettings& settings, double samplesPerPixel,
size_t len_, const SpectrogramSettings& settings, double samplesPerPixel,

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This AI slop is also wrong. This method does modify settings. Unbelievable

@haileys haileys closed this Jun 26, 2025
@haileys haileys deleted the audacity3+fix-spectrogram-crash branch June 26, 2025 23:31
@haileys
Copy link
Copy Markdown
Author

haileys commented Jun 26, 2025

I'll send this one to Tenacity instead.

@kryksyh
Copy link
Copy Markdown
Member

kryksyh commented Jun 27, 2025

@haileys, I approved your PR, and it was assigned to QA for testing before it could be merged. No decisions were made based on AI "analysis".

In any case we respect your choice, and thanks for the report.

@joepie91
Copy link
Copy Markdown

@kryksyh It's quite disrespectful to send volunteer contributors onto a wild goose hunt of AI slop "reviews". Whether or not decisionmaking is based on it in the end, what you are communicating to contributors is "you need to fix these things that the AI told you about", which is not at all respectful of their time.

(I'll elide all the other technical and moral issues with "AI" since people have written extensively about those already)

@kryksyh
Copy link
Copy Markdown
Member

kryksyh commented Jun 27, 2025

, what you are communicating to contributors is "you need to fix these things that the AI told you about", which is not at all respectful of their time.

Yes, blame on me, you are right to point this out and I had to "resolve" those messages when I checked them and saw that they are incorrect.

On the other hand, the PR was approved which is a direct communication that no further action is required.

@margaretjoanmiller
Copy link
Copy Markdown

is there a way then to turn off copilot's "helpful" diagnostics when the PR is already approved then?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Crash when rendering spectrum view

5 participants