Skip to content

fix: Potential fix for code scanning alert no. 19: Slice memory allocation with excessive size value#9

Merged
skyoo2003 merged 2 commits into
mainfrom
alert-autofix-19
Apr 20, 2026
Merged

fix: Potential fix for code scanning alert no. 19: Slice memory allocation with excessive size value#9
skyoo2003 merged 2 commits into
mainfrom
alert-autofix-19

Conversation

@skyoo2003
Copy link
Copy Markdown
Owner

Potential fix for https://github.com/skyoo2003/devcloud/security/code-scanning/19

General fix: enforce strict upper/lower bounds before any slice allocation that depends on variable size, and ensure constructor parameters cannot create invalid or oversized internal buffers.

Best fix here (without changing intended behavior):

  1. In internal/dashboard/logger.go, harden NewLogCollector(maxSize int) by clamping invalid/oversized maxSize to safe bounds.
  2. In Recent(n int), clamp n not only to c.count but also to c.maxSize before allocation.
  3. Introduce local constants for sane defaults/maximums in this file so limits are explicit and centralized.

This preserves functionality (ring buffer + recent retrieval) while preventing excessive allocations even if callers pass unexpected values.

Suggested fixes powered by Copilot Autofix. Review carefully before merging.

… with excessive size value

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai Bot commented Apr 19, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Hardens the dashboard request logging ring buffer against excessive or invalid slice allocations by clamping constructor and Recent() arguments to explicit size limits, using centralized constants for default and maximum capacity.

File-Level Changes

Change Details Files
Add centralized size limits for the LogCollector to constrain internal buffer allocations.
  • Introduce defaultLogCollectorSize and maxLogCollectorSize constants to define sane defaults and upper bounds for log collector capacity
  • Use these constants when normalizing size-related values in the logger implementation
internal/dashboard/logger.go
Harden LogCollector construction to prevent invalid or unbounded internal slice sizes.
  • Clamp maxSize to defaultLogCollectorSize when callers pass zero or negative values
  • Clamp maxSize to maxLogCollectorSize when callers pass values above the supported maximum
  • Ensure entries slice capacity is always within validated bounds during LogCollector initialization
internal/dashboard/logger.go
Constrain Recent() retrieval size to avoid excessive slice allocations even with large caller-provided n values.
  • Clamp n to c.maxSize before allocating the result slice in Recent
  • Preserve existing behavior that also clamps n to c.count so returned logs never exceed the number of stored entries
internal/dashboard/logger.go

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@skyoo2003 skyoo2003 changed the title Potential fix for code scanning alert no. 19: Slice memory allocation with excessive size value fix: Potential fix for code scanning alert no. 19: Slice memory allocation with excessive size value Apr 19, 2026
@skyoo2003 skyoo2003 self-assigned this Apr 19, 2026
@skyoo2003 skyoo2003 marked this pull request as ready for review April 19, 2026 18:51
Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

Hey - I've left some high level feedback:

  • In Recent, consider clamping or early-returning when n <= 0 to avoid surprising behavior or potential panics if a negative value is passed in.
  • The n > c.maxSize check in Recent may be redundant if c.count is already guaranteed to never exceed c.maxSize; if that invariant holds, you could simplify by only clamping to c.count.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `Recent`, consider clamping or early-returning when `n <= 0` to avoid surprising behavior or potential panics if a negative value is passed in.
- The `n > c.maxSize` check in `Recent` may be redundant if `c.count` is already guaranteed to never exceed `c.maxSize`; if that invariant holds, you could simplify by only clamping to `c.count`.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Early-return on n <= 0 or empty buffer, and remove redundant
n > c.maxSize check since c.count is already capped at maxSize.
@github-actions github-actions Bot added the bug Something isn't working label Apr 20, 2026
@skyoo2003 skyoo2003 merged commit 305ecb8 into main Apr 20, 2026
10 checks passed
@skyoo2003 skyoo2003 deleted the alert-autofix-19 branch April 20, 2026 16:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant