|
| 1 | +// |
| 2 | +// Test program for pdftoraster copy_height off-by-one fix. |
| 3 | +// |
| 4 | +// This test validates the copy loop logic used in write_page_image() |
| 5 | +// when the rendered image is smaller than the page height. The bug |
| 6 | +// used `h <= copy_height` instead of `h < copy_height`, causing one |
| 7 | +// extra iteration past the allocated buffer. The fix also adds a guard |
| 8 | +// for `allocLineBuf` to prevent NULL-ptr memset. |
| 9 | +// |
| 10 | +// Licensed under Apache License v2.0. See the file "LICENSE" for more |
| 11 | +// information. |
| 12 | +// |
| 13 | + |
| 14 | +#include <stdio.h> |
| 15 | +#include <stdlib.h> |
| 16 | +#include <string.h> |
| 17 | + |
| 18 | +static int n_errors = 0; |
| 19 | +#define TEST(cond, msg) do { \ |
| 20 | + if (!(cond)) { \ |
| 21 | + fprintf(stderr, "FAIL: %s\n", msg); \ |
| 22 | + n_errors++; \ |
| 23 | + } else { \ |
| 24 | + fprintf(stderr, "OK: %s\n", msg); \ |
| 25 | + } \ |
| 26 | +} while (0) |
| 27 | + |
| 28 | +typedef struct { |
| 29 | + unsigned int cupsHeight; |
| 30 | + unsigned int cupsWidth; |
| 31 | + unsigned int bytesPerLine; |
| 32 | + int allocLineBuf; |
| 33 | +} doc_t; |
| 34 | + |
| 35 | +/* Simulate the forward copy loop from write_page_image(). |
| 36 | + * Returns the number of times the "inside valid page/image area" branch executed. |
| 37 | + * If allocLineBuf is 0 and the branch would execute, returns 0 on crash (caller |
| 38 | + * interprets). */ |
| 39 | +static int |
| 40 | +simulate_forward_loop(unsigned int image_height, |
| 41 | + unsigned int page_height, |
| 42 | + int allocLineBuf) |
| 43 | +{ |
| 44 | + doc_t doc; |
| 45 | + memset(&doc, 0, sizeof(doc)); |
| 46 | + doc.cupsHeight = page_height; |
| 47 | + doc.cupsWidth = 16; |
| 48 | + doc.bytesPerLine = 16; |
| 49 | + doc.allocLineBuf = allocLineBuf; |
| 50 | + |
| 51 | + unsigned int copy_height = (image_height < page_height) ? image_height : page_height; |
| 52 | + unsigned char *colordata = (unsigned char *)malloc(image_height * 16); |
| 53 | + unsigned char *lineBuf = NULL; |
| 54 | + if (allocLineBuf) |
| 55 | + lineBuf = (unsigned char *)malloc(doc.bytesPerLine); |
| 56 | + |
| 57 | + unsigned char *bp = colordata; |
| 58 | + int inside_count = 0; |
| 59 | + |
| 60 | + for (unsigned int h = 0; h < doc.cupsHeight; h++) |
| 61 | + { |
| 62 | + if (h < copy_height) // FIXED: was h <= copy_height |
| 63 | + { |
| 64 | + inside_count++; |
| 65 | + if (doc.allocLineBuf) |
| 66 | + memset(lineBuf, 0, doc.bytesPerLine); |
| 67 | + bp += 16; // image_rowsize |
| 68 | + } |
| 69 | + else |
| 70 | + { |
| 71 | + if (doc.allocLineBuf) |
| 72 | + memset(lineBuf, 0, doc.bytesPerLine); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /* bp should never point past the allocated region. |
| 77 | + * After the fix: copy_height iterations, each advancing by 16, |
| 78 | + * so bp == colordata + copy_height * 16 == end of allocation. */ |
| 79 | + if (bp > colordata + image_height * 16) { |
| 80 | + fprintf(stderr, "FAIL: bp=%p exceeds colordata+size=%p (copy_height=%u image_height=%u)\n", |
| 81 | + (void*)bp, (void*)(colordata + image_height * 16), copy_height, image_height); |
| 82 | + n_errors++; |
| 83 | + } |
| 84 | + |
| 85 | + free(lineBuf); |
| 86 | + free(colordata); |
| 87 | + return inside_count; |
| 88 | +} |
| 89 | + |
| 90 | +/* Simulate the reverse (duplex) copy loop. |
| 91 | + * |
| 92 | + * The reverse loop iterates h from page_height DOWN TO 1, with bp |
| 93 | + * starting at the last valid row. Here h <= copy_height is correct |
| 94 | + * (unlike the forward loop) because h = copy_height corresponds to |
| 95 | + * the last valid row index (copy_height-1), and the loop ends before |
| 96 | + * bp can be dereferenced past the allocation start. |
| 97 | + */ |
| 98 | +static int |
| 99 | +simulate_reverse_loop(unsigned int image_height, |
| 100 | + unsigned int page_height, |
| 101 | + int allocLineBuf) |
| 102 | +{ |
| 103 | + doc_t doc; |
| 104 | + memset(&doc, 0, sizeof(doc)); |
| 105 | + doc.cupsHeight = page_height; |
| 106 | + doc.cupsWidth = 16; |
| 107 | + doc.bytesPerLine = 16; |
| 108 | + doc.allocLineBuf = allocLineBuf; |
| 109 | + |
| 110 | + unsigned int copy_height = (image_height < page_height) ? image_height : page_height; |
| 111 | + unsigned char *colordata = (unsigned char *)malloc(image_height * 16); |
| 112 | + unsigned char *lineBuf = NULL; |
| 113 | + if (allocLineBuf) |
| 114 | + lineBuf = (unsigned char *)malloc(doc.bytesPerLine); |
| 115 | + |
| 116 | + unsigned char *bp = colordata + (copy_height - 1) * 16; |
| 117 | + int inside_count = 0; |
| 118 | + |
| 119 | + for (unsigned int h = doc.cupsHeight; h > 0; h--) |
| 120 | + { |
| 121 | + if (h <= copy_height) // correct (h descends from page_height to 1) |
| 122 | + { |
| 123 | + inside_count++; |
| 124 | + if (doc.allocLineBuf) |
| 125 | + memset(lineBuf, 0, doc.bytesPerLine); |
| 126 | + bp -= 16; |
| 127 | + } |
| 128 | + else |
| 129 | + { |
| 130 | + if (doc.allocLineBuf) |
| 131 | + memset(lineBuf, 0, doc.bytesPerLine); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + free(lineBuf); |
| 136 | + free(colordata); |
| 137 | + return inside_count; |
| 138 | +} |
| 139 | + |
| 140 | +int |
| 141 | +main(void) |
| 142 | +{ |
| 143 | + fprintf(stderr, "--- pdftoraster copy_height tests ---\n"); |
| 144 | + |
| 145 | + /* Test 1: image < page, allocLineBuf=true — the OOB scenario */ |
| 146 | + TEST(simulate_forward_loop(1, 2, 1) == 1, |
| 147 | + "forward: image=1 page=2 -> inside_count=1 (was 2 before fix)"); |
| 148 | + TEST(simulate_reverse_loop(1, 2, 1) == 1, |
| 149 | + "reverse: image=1 page=2 -> inside_count=1 (was 2 before fix)"); |
| 150 | + |
| 151 | + /* Test 2: image == page — normal case */ |
| 152 | + TEST(simulate_forward_loop(5, 5, 1) == 5, |
| 153 | + "forward: image=5 page=5 -> inside_count=5"); |
| 154 | + TEST(simulate_reverse_loop(5, 5, 1) == 5, |
| 155 | + "reverse: image=5 page=5 -> inside_count=5"); |
| 156 | + |
| 157 | + /* Test 3: image > page — normal case (clamped by copy_height) */ |
| 158 | + TEST(simulate_forward_loop(10, 5, 1) == 5, |
| 159 | + "forward: image=10 page=5 -> inside_count=5 (clamped)"); |
| 160 | + TEST(simulate_reverse_loop(10, 5, 1) == 5, |
| 161 | + "reverse: image=10 page=5 -> inside_count=5 (clamped)"); |
| 162 | + |
| 163 | + /* Test 4: image=0 (edge case: should not crash) */ |
| 164 | + /* copy_height = 0, loop should never enter the "inside" branch */ |
| 165 | + TEST(simulate_forward_loop(0, 5, 1) == 0, |
| 166 | + "forward: image=0 page=5 -> inside_count=0"); |
| 167 | + TEST(simulate_reverse_loop(0, 5, 1) == 0, |
| 168 | + "reverse: image=0 page=5 -> inside_count=0"); |
| 169 | + |
| 170 | + /* Test 5: image < page, allocLineBuf=false — the NULL-ptr crash scenario */ |
| 171 | + /* Must not crash; function returns early from inside branch since allocLineBuf=false */ |
| 172 | + TEST(simulate_forward_loop(1, 2, 0) == 1, |
| 173 | + "forward: image=1 page=2 allocLineBuf=0 -> no crash, inside_count=1"); |
| 174 | + TEST(simulate_reverse_loop(1, 2, 0) == 1, |
| 175 | + "reverse: image=1 page=2 allocLineBuf=0 -> no crash, inside_count=1"); |
| 176 | + |
| 177 | + fprintf(stderr, "--- %s ---\n", n_errors ? "SOME TESTS FAILED" : "ALL TESTS PASSED"); |
| 178 | + return n_errors ? 1 : 0; |
| 179 | +} |
0 commit comments