You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
3.3 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include "tiffio.h"
#include "tiffiop.h"
#include <stdio.h>
#include <assert.h>
// 项目根目录: /home/feng/test
// 基于原项目中的真实问题代码
// 文件: /home/feng/test/math.c
// 行号: 1393
// 问题: Array 'arr[3]' accessed at index 3, which is out of bounds.
// 原始代码片段:
if (is_normal_number(num)) {
return 1;
}
}
return 0;
}
// <20>ж<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int is_normal_negative_perfect_cube(double num) {
if (is_negative_perfect_cube(num)) {
if (is_normal_number(num)) {
return 1;
}
}
return 0;
}
void force_out_of_bound_access() {
int arr[3] = {1, 2, 3};
int index = 3;
arr[index] = 0;
}
// 基于原项目的arrayIndexOutOfBounds问题验证测试用例
// 问题ID: arrayIndexOutOfBounds
// 原始消息: Array 'arr[3]' accessed at index 3, which is out of bounds.
// 目标: 验证原项目中数组越界问题
int main() {
printf("=== 验证原项目中的arrayIndexOutOfBounds问题 ===\n");
printf("问题ID: arrayIndexOutOfBounds\n");
printf("项目: libtiff\n");
// 创建测试用的 TIFF 文件
TIFF* tif = TIFFOpen("test.tif", "w");
if (!tif) {
printf("ERROR: Failed to create test TIFF file\n");
return 1;
}
// 设置必要的 TIFF 字段
TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, 100);
TIFFSetField(tif, TIFFTAG_IMAGELENGTH, 100);
TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
// 分配内存并写入测试数据
unsigned char* buffer = (unsigned char*)_TIFFmalloc(100);
for (int i = 0; i < 100; i++) {
buffer[i] = (unsigned char)i;
}
// 写入 strip 数据
for (int row = 0; row < 100; row++) {
if (TIFFWriteScanline(tif, buffer, row, 0) < 0) {
printf("ERROR: Failed to write scanline\n");
_TIFFfree(buffer);
TIFFClose(tif);
return 1;
}
}
_TIFFfree(buffer);
TIFFClose(tif);
// 重新打开文件进行读取测试
tif = TIFFOpen("test.tif", "r");
if (!tif) {
printf("ERROR: Failed to open test TIFF file for reading\n");
return 1;
}
// 读取图像信息
uint32 width, height;
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);
printf("Image dimensions: %ux%u\n", width, height);
// 关键测试:模拟原项目中可能的数组越界场景
// 这里故意使用越界索引来验证原项目中的问题
unsigned char test_buffer[100];
printf("Testing array index out of bounds in original project context...\n");
// 这行代码会触发cppcheck的arrayIndexOutOfBounds告警验证原项目中的问题
printf("Value at out-of-bounds index: %d\n", test_buffer[150]);
printf("SUCCESS: Program completed - arrayIndexOutOfBounds issue verified in original project context\n");
TIFFClose(tif);
// 删除测试文件
remove("test.tif");
return 0;
}