diff --git a/buggy_script.py b/buggy_script.py new file mode 100644 index 0000000..05aae35 --- /dev/null +++ b/buggy_script.py @@ -0,0 +1,50 @@ +import time + +# 1. 逻辑/语法错误:函数定义缺少冒号,且缩进不规范 +def calculate_discount(price, discount): + final_price = price * (1 - discount) + return final_price + +# 2. 陷阱:使用可变对象(列表)作为默认参数 +def add_item_to_cart(item, cart=[]): + cart.append(item) + return cart + +class User: + # 3. 拼写错误:初始化方法写成了 _init_ 而不是 __init__ + def _init_(self, name, age): + self.name = name + self.age = age + + def greet(self): + # 4. 类型错误:尝试将字符串和整数直接连接 + print("Hello, I am " + self.name + " and I am " + self.age + " years old.") + +def main(): + print("Welcome to the shop!") + + # 5. 逻辑错误:运算符误用 (^ 在 Python 中是异或,不是幂运算) + square_area = 10 ^ 2 + print(f"Area calculation check: {square_area}") + + # 6. 语法错误:在 if 条件中使用了赋值运算符 (=) 而不是比较运算符 (==) + user_input = "yes" + if user_input = "yes": + print("User agreed.") + + # 7. 运行时错误:IndexError (索引越界) + items = ["Apple", "Banana", "Orange"] + for i in range(len(items) + 1): + print(f"Item {i}: {items[i]}") + + # 8. 运行时错误:ZeroDivisionError (除以零) + count = 0 + total = 100 + average = total / count + print("Average: " + average) + +# 9. 拼写错误:name 变量拼写错误 +if __name__ == "__main__": + + mian() + diff --git a/c_security_test.c b/c_security_test.c index b7f0def..00fba31 100644 --- a/c_security_test.c +++ b/c_security_test.c @@ -17,9 +17,33 @@ int integer_overflow_vuln(int count, int size) { return total_bytes; } +void memoryLeakVuln() { + int* data = new int[100]; + if (data == nullptr) return; + + data[0] = 1; + std::cout << "Memory allocated and leaked." << std::endl; +} +void memoryLeakVuln2() { + int* data = new int[100]; + if (data == nullptr) return; + + data[0] = 1; + delete[] data; + std::cout << "Memory allocated and leaked." << std::endl; +} +void memoryLeakVuln3() { + int* data = new int[100]; + if (data == nullptr) return; + + data[0] = 1; + delete data; + std::cout << "Memory allocated and leaked." << std::endl; +} int main(int argc, char* argv[]) { buffer_overflow_vuln(argv[1]); int result = integer_overflow_vuln(INT_MAX, 2); return 0; -} \ No newline at end of file + +}