Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,7 @@ from __future__ import annotations

import math
import os
from typing import TYPE_CHECKING

if TYPE_CHECKING:
pass


def restaurant(l: int, b: int) -> int:
Expand Down Expand Up @@ -298,12 +295,12 @@ if __name__ == '__main__':
### 定数倍削減テクニック

```python
# ❌ 非効率: 中間変数の無駄な生成
# 可読性重視: 中間変数を使った例
area = l * b
square_area = g * g
result = area // square_area

# ✅ 最適: 1行で計算
# より簡潔に書ける
result = (l * b) // (g * g)
```

Expand Down
21 changes: 20 additions & 1 deletion calc_sri_fix.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
#!/bin/bash

set -euo pipefail

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

cat calc_sri_fix.sh

Repository: myoshi2891/Algorithm-DataStructures-Math-SQL

Length of output: 948


set -euo pipefail の追加により、厳格なシェルモードが有効になりました。

-e オプションによるエラー発生時の即座の終了は、スクリプトの堅牢性を向上させます。Line 28 の calculate_sri が失敗した場合、スクリプト全体が終了し Line 29 の呼び出しが実行されないのは、エラー時の適切なフェイルファーストの動作です。

ただし、重要な問題として、Line 23 のパイプライン内で openssl コマンドが失敗した場合、set -e により即座にスクリプトが終了するため、Line 25 の rm -f "$temp_file" が実行されず、一時ファイルがクリーンアップされないままになります。 この問題を解決するには、関数内で以下のようなトラップを設定してください:

trap "rm -f \"$temp_file\"" RETURN

このトラップを関数の先頭に追加することで、予期しない終了時にも一時ファイルが確実にクリーンアップされます。

🤖 Prompt for AI Agents
In `@calc_sri_fix.sh` at line 3, The strict shell mode (set -euo pipefail) can
leave the temp file behind if openssl in the pipeline fails; inside the
calculate_sri function, add a cleanup trap so the temp file is always removed on
function exit (e.g., trap "rm -f \"$temp_file\"" RETURN placed at the top of
calculate_sri), ensure temp_file is defined before the trap is set, and keep the
existing rm -f "$temp_file" at the end for normal-path cleanup.


calculate_sri() {
url="$1"
hash=$(curl -sL "$url" | openssl dgst -sha384 -binary | openssl base64 -A)
temp_file=$(mktemp)

# curl options: -f (fail on HTTP error), -S (show error), -s (silent equivalent), -L (follow redirects)
if ! curl -fS -sL "$url" -o "$temp_file"; then
echo "Error downloading $url" >&2
rm -f "$temp_file"
return 1
fi

# Check for empty response
if [ ! -s "$temp_file" ]; then
echo "Error: Empty response from $url" >&2
rm -f "$temp_file"
return 1
fi

hash=$(openssl dgst -sha384 -binary < "$temp_file" | openssl base64 -A)
echo "$url sha384-$hash"
rm -f "$temp_file"
Comment on lines +7 to +25

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

一時ファイルのクリーンアップに trap の使用を推奨します。

Line 23 の openssl コマンドが失敗した場合、set -e によりスクリプトが即座に終了し、Line 25 の rm -f "$temp_file" が実行されずに一時ファイルがリークします。関数冒頭で trap を設定することで、異常終了時も確実にクリーンアップできます。

🛡️ trap を使った修正案
 calculate_sri() {
     url="$1"
     temp_file=$(mktemp)
+    trap 'rm -f "$temp_file"' RETURN
 
     # curl options: -f (fail on HTTP error), -S (show error), -s (silent equivalent), -L (follow redirects)
     if ! curl -fS -sL "$url" -o "$temp_file"; then
         echo "Error downloading $url" >&2
-        rm -f "$temp_file"
         return 1
     fi
 
     # Check for empty response
     if [ ! -s "$temp_file" ]; then
         echo "Error: Empty response from $url" >&2
-        rm -f "$temp_file"
         return 1
     fi
 
     hash=$(openssl dgst -sha384 -binary < "$temp_file" | openssl base64 -A)
     echo "$url sha384-$hash"
-    rm -f "$temp_file"
 }

注意: trap ... RETURN は Bash 4.0+ で利用可能です。もし互換性が必要な場合は、trap ... EXIT をスクリプトのトップレベルで設定するか、各パスでの手動 rm を維持しつつ openssl 行を if で囲む方法も有効です。

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
temp_file=$(mktemp)
# curl options: -f (fail on HTTP error), -S (show error), -s (silent equivalent), -L (follow redirects)
if ! curl -fS -sL "$url" -o "$temp_file"; then
echo "Error downloading $url" >&2
rm -f "$temp_file"
return 1
fi
# Check for empty response
if [ ! -s "$temp_file" ]; then
echo "Error: Empty response from $url" >&2
rm -f "$temp_file"
return 1
fi
hash=$(openssl dgst -sha384 -binary < "$temp_file" | openssl base64 -A)
echo "$url sha384-$hash"
rm -f "$temp_file"
temp_file=$(mktemp)
trap 'rm -f "$temp_file"' RETURN
# curl options: -f (fail on HTTP error), -S (show error), -s (silent equivalent), -L (follow redirects)
if ! curl -fS -sL "$url" -o "$temp_file"; then
echo "Error downloading $url" >&2
return 1
fi
# Check for empty response
if [ ! -s "$temp_file" ]; then
echo "Error: Empty response from $url" >&2
return 1
fi
hash=$(openssl dgst -sha384 -binary < "$temp_file" | openssl base64 -A)
echo "$url sha384-$hash"
🤖 Prompt for AI Agents
In `@calc_sri_fix.sh` around lines 7 - 25, The temporary file created in this
function (temp_file via mktemp) can leak if a command like the openssl pipeline
fails under set -e; add a trap at the start of the function to ensure rm -f
"$temp_file" runs on function exit (use trap 'rm -f "$temp_file"' RETURN for
Bash 4+ or trap 'rm -f "$temp_file"' EXIT at script top-level for wider
compatibility), then remove the final explicit rm -f "$temp_file" or keep it as
a no-op; apply the change around the block that downloads with curl and computes
the hash (the temp_file variable and the openssl dgst ... | openssl base64 -A
pipeline).

}

calculate_sri "https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/plugins/line-numbers/prism-line-numbers.min.js"
Expand Down