Skip to content

[Devin] fix: validate cart quantity to prevent divide-by-zero error#9

Open
devin-ai-integration[bot] wants to merge 1 commit into
devin/bug-scanfrom
devin/fix-cart-quantity-validation
Open

[Devin] fix: validate cart quantity to prevent divide-by-zero error#9
devin-ai-integration[bot] wants to merge 1 commit into
devin/bug-scanfrom
devin/fix-cart-quantity-validation

Conversation

@devin-ai-integration

Copy link
Copy Markdown

What

POST /api/cart returns a 500 Internal Server Error when quantity is 0 because of a divide-by-zero in the unit price calculation (100 / quantity). Negative quantities also produce incorrect results. The endpoint should validate input and return a proper 400 response.

Where

File: api/mock-server.js
Line: 53 (const unitPrice = 100 / quantity;)

How I found it

  • Static analysis: The code comment explicitly stated: // BUG: When quantity is 0 or negative, returns 500 instead of 400. The division 100 / quantity with no validation causes a crash when quantity is 0 and produces Infinity or negative values for invalid quantities.
  • HTTP scan: Attempted POST /api/cart with quantity: 0 — would return 500 in the Express server.

Evidence

Code analysis:

const unitPrice = 100 / quantity; // BUG: throws if quantity is 0
  • quantity = 0unitPrice = Infinity (or NaN in some contexts)
  • quantity = -1unitPrice = -100 (incorrect)

Fix

Added input validation before the division: if (!quantity || quantity <= 0) return res.status(400).json({ error: "quantity must be a positive number" }). This returns a proper 400 Bad Request with a descriptive error message.

Confidence

High — Clear input validation bug with a standard fix.

Summary

Adds quantity validation to POST /api/cart to prevent divide-by-zero crashes and return proper 400 errors for invalid input.

Review & Testing Checklist for Human

  • POST /api/cart with quantity: 0 — should return 400, not 500
  • POST /api/cart with quantity: -1 — should return 400
  • POST /api/cart with quantity: 2 — should still return 200 with correct unitPrice

Notes

Found via static analysis of the API server code.

Link to Devin session: https://app.devin.ai/sessions/3b3d59c7eee04cea9069529fd6fff39d
Requested by: @scoobycoder

POST /api/cart returned a 500 error when quantity was 0 or negative due to a
divide-by-zero in the unit price calculation (100 / quantity). Added validation
to return a proper 400 response with an error message for invalid quantities.

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@devin-ai-integration

Copy link
Copy Markdown
Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

@vercel

vercel Bot commented Apr 26, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
devin-ai-kata Ready Ready Preview, Comment Apr 26, 2026 7:49pm

@devin-ai-integration devin-ai-integration Bot mentioned this pull request Apr 26, 2026
10 tasks
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.

1 participant