Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion usermods/TetrisAI_v2/gridbw.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#ifndef __GRIDBW_H__
#define __GRIDBW_H__

#include <iterator>
#include <vector>
#include "pieces.h"

Expand Down
1 change: 0 additions & 1 deletion usermods/TetrisAI_v2/pieces.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include <bitset>
#include <cstddef>
#include <cassert>
#include <iostream>

#define numPieces 7

Expand Down
12 changes: 3 additions & 9 deletions usermods/TetrisAI_v2/tetrisbag.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

#include <stdint.h>
#include <vector>
#include <algorithm>

#include "tetrisbag.h"

Expand Down Expand Up @@ -87,17 +86,12 @@ class TetrisBag
void queuePiece()
{
//move vector to left
std::rotate(piecesQueue.begin(), piecesQueue.begin() + 1, piecesQueue.end());
for (uint8_t i = 1; i < piecesQueue.size(); i++) {
piecesQueue[i - 1] = piecesQueue[i];
}
piecesQueue[piecesQueue.size() - 1] = getNextPiece();
Comment on lines 86 to 92
Copy link
Contributor

@coderabbitai coderabbitai bot Jan 24, 2026

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Guard against empty queue to avoid out-of-range access.

If queueLength == 0, piecesQueue.size() - 1 underflows and the assignment is out-of-bounds. Add a quick empty check (or assert) to keep this safe.

🐛 Suggested fix
 void queuePiece()
 {
+    if (piecesQueue.empty()) return;
     //move vector to left
     for (uint8_t i = 1; i < piecesQueue.size(); i++) {
         piecesQueue[i - 1] = piecesQueue[i];
     }
     piecesQueue[piecesQueue.size() - 1] = getNextPiece();
 }
🤖 Prompt for AI Agents
In `@usermods/TetrisAI_v2/tetrisbag.h` around lines 86 - 92, queuePiece() can
underflow when the queue is empty because piecesQueue.size() - 1 will wrap and
cause out-of-range access; fix by returning early (or asserting) when the queue
is empty (e.g., check piecesQueue.empty() or queueLength == 0) before shifting
elements and assigning getNextPiece(), ensuring you guard the piecesQueue access
in queuePiece().

Copy link
Member

Choose a reason for hiding this comment

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

Disregard, for loop is never entered if piecesQueue.size() < 1

Copy link
Contributor

Choose a reason for hiding this comment

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

@Aircoookie You're absolutely right that the for loop won't execute when the queue is empty. I was also concerned about the assignment on the next line (piecesQueue[piecesQueue.size() - 1] = getNextPiece();), which could underflow if size() == 0. However, if the queue is always initialized with a non-zero size in practice, that wouldn't be an issue. I'll defer to your judgment on this! 👍

}

void queuePiece(uint8_t idx)
{
//move vector to left
std::rotate(piecesQueue.begin(), piecesQueue.begin() + 1, piecesQueue.end());
piecesQueue[piecesQueue.size() - 1] = Piece(idx % nPieces);
}

void reset()
{
bag.clear();
Expand Down