Skip to content
Open
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
10 changes: 5 additions & 5 deletions bin/idstack-learnings-search
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ if command -v python3 &>/dev/null; then
python3 -c "
import json, sys

sources = '$SOURCES'.split()
type_filter = '$TYPE'
keyword = '$KEYWORD'.lower()
limit = $LIMIT
sources = sys.argv[1].split()
type_filter = sys.argv[2]
keyword = sys.argv[3].lower()
limit = int(sys.argv[4])

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

If LIMIT is empty (e.g., if passed as --limit "") or contains a non-integer value, int(sys.argv[4]) will raise a ValueError, causing the Python script to crash and fall back to the grep/tail implementation (which may also fail or behave unexpectedly).

We should wrap the integer conversion in a try-except block to fall back to a safe default value (like 3).

try:
    limit = int(sys.argv[4])
except ValueError:
    limit = 3


matches = []
for src in sources:
Expand All @@ -69,7 +69,7 @@ for src in sources:
# Local learnings first (take precedence), then global
for m in matches[-limit:]:
print(m)
" 2>/dev/null || {
" "$SOURCES" "$TYPE" "$KEYWORD" "$LIMIT" 2>/dev/null || {
# Fallback: basic grep
if [ -n "$KEYWORD" ]; then
cat $SOURCES 2>/dev/null | grep -i "$KEYWORD" | tail -"$LIMIT"
Expand Down