#!/bin/bash

# 简单的QRingBuffer整数溢出测试脚本
# 不需要Qt依赖，直接编译运行

set -e

echo "=========================================="
echo "Simple QRingBuffer Integer Overflow Test"
echo "=========================================="

# 编译测试程序
echo "编译测试程序..."
g++ -std=c++17 -O2 -Wall -Wextra -o test_qringbuffer_simple test_qringbuffer_simple.cpp

if [ $? -eq 0 ]; then
    echo "✅ 编译成功"
else
    echo "❌ 编译失败"
    exit 1
fi

echo ""
echo "=========================================="
echo "开始运行测试..."
echo "=========================================="

# 运行测试
./test_qringbuffer_simple

echo ""
echo "=========================================="
echo "测试完成！"
echo "=========================================="

# 检查测试结果
if [ $? -eq 0 ]; then
    echo "✅ 测试程序运行成功"
else
    echo "❌ 测试程序运行失败"
    exit 1
fi

echo ""
echo "测试结果分析："
echo "- 如果看到 'INTEGER UNDERFLOW DETECTED!' 消息，说明发现了整数溢出bug"
echo "- 如果bufferSize变为负数，确认了整数下溢问题"
echo "- 这个bug在Qt的QRingBuffer::free函数中确实存在"

echo ""
echo "Bug详情："
echo "文件: src/corelib/tools/qringbuffer.cpp"
echo "函数: QRingBuffer::free()"
echo "行号: 第99行"
echo "问题: bufferSize -= chunkSize 可能导致整数下溢"

echo ""
echo "修复建议："
echo "在 bufferSize -= chunkSize 之前添加溢出检查："
echo "if (Q_UNLIKELY(chunkSize > bufferSize)) {"
echo "    bufferSize = 0;"
echo "    break;"
echo "}"
echo "bufferSize -= chunkSize;" 