Files
ds4/tests/test_image_decode.c
T
Salvatore Sanfilippo 62867e913a Make JPEG regression failure diagnostics safe
Always compute the decoded fingerprint before checking dimensions, so the error path cannot print an uninitialized buffer. Follow-up to #981.
2026-09-18 09:54:34 +02:00

50 lines
1.8 KiB
C

#include "ds4_image.h"
#include <stdio.h>
#include <string.h>
static int hex_fingerprint(const uint8_t *fp, char *out, size_t cap) {
if (cap < 65) return 0;
for (int i = 0; i < 32; i++)
snprintf(out + i * 2, cap - (size_t)i * 2, "%02x", fp[i]);
return 1;
}
static int check_jpeg(const char *path, uint32_t width, uint32_t height,
const char *expected_fp) {
ds4_image image = {0};
char error[160] = {0};
if (!ds4_image_decode_file(&image, path, error, sizeof(error))) {
fprintf(stderr, "decode failed for %s: %s\n", path, error);
return 0;
}
char got[65] = {0};
int ok = hex_fingerprint(image.fingerprint, got, sizeof(got)) &&
image.width == width && image.height == height &&
strcmp(got, expected_fp) == 0;
if (!ok) {
fprintf(stderr, "%s: got %ux%u fp=%s, expected %ux%u fp=%s\n",
path, image.width, image.height, got,
width, height, expected_fp);
}
ds4_image_free(&image);
return ok;
}
int main(void) {
/* 24x16 grayscale progressive JPEG. Unpatched Iris yields different
* pixels; patched decode matches libjpeg-turbo djpeg bit-exactly. */
if (!check_jpeg("tests/vision-fixtures/jpeg/prog_ac_refine_zrl_gray.jpg",
24u, 16u,
"63aae8863e829170c5abc746c90a5ff3ad60e9c4312e2ffa5eefbfeaacd26bfc"))
return 1;
/* 64x48 4:2:0 progressive JPEG. Unpatched jpeg_load returns NULL.
* After the ZRL fix plus main's chroma interpolation, decode matches
* libjpeg-turbo djpeg bit-exactly. */
if (!check_jpeg("tests/vision-fixtures/jpeg/prog_ac_refine_zrl_420.jpg",
64u, 48u,
"d3be4d7078c41b6589942c82bd622ca8a3ed40adddee11cccf1de9ca1a096ba4"))
return 1;
return 0;
}