Skip to content

Commit 0bf64e9

Browse files
authored
fix matrixload sniff for some matrix files (#4372)
matrixload is_a could fail to detect files like this: ``` 2 2 0 0 1 1 ``` The header parser was not stopping at EOL and thought that this file had scale 0, which it would then reject.
1 parent 516fee5 commit 0bf64e9

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

ChangeLog

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
- fill_nearest: fix a leak
2020
- colour: use suggested rendering intent as fallback [kleisauke]
2121
- morph: fix Orc path with large masks [kleisauke]
22+
- matrixload: fix file format detect for some matrix types
2223

2324
10/10/24 8.16.0
2425

libvips/foreign/matrixload.c

+11-12
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,15 @@ parse_matrix_header(char *line,
119119
char *p, *q;
120120
int i;
121121

122-
for (i = 0, p = line;
123-
(q = vips_break_token(p, " \t")) &&
124-
i < 4;
125-
i++, p = q)
122+
/* Stop at newline.
123+
*/
124+
if ((p = strchr(line, '\r')) ||
125+
((p = strchr(line, '\n'))))
126+
*p = '\0';
127+
128+
for (i = 0, p = line; (q = vips_break_token(p, " \t")) && i < 4; i++, p = q)
126129
if (vips_strtod(p, &header[i])) {
127-
vips_error("matload",
128-
_("bad number \"%s\""), p);
130+
vips_error("matload", _("bad number \"%s\""), p);
129131
return -1;
130132
}
131133

@@ -152,8 +154,7 @@ parse_matrix_header(char *line,
152154
*width > 100000 ||
153155
*height <= 0 ||
154156
*height > 100000) {
155-
vips_error("mask2vips",
156-
"%s", _("width / height out of range"));
157+
vips_error("mask2vips", "%s", _("width / height out of range"));
157158
return -1;
158159
}
159160
if (header[2] == 0.0) {
@@ -426,14 +427,12 @@ vips_foreign_load_matrix_source_is_a_source(VipsSource *source)
426427
double offset;
427428
int result;
428429

429-
if ((bytes_read = vips_source_sniff_at_most(source,
430-
&data, 79)) <= 0)
430+
if ((bytes_read = vips_source_sniff_at_most(source, &data, 79)) <= 0)
431431
return FALSE;
432432
g_strlcpy(line, (const char *) data, 80);
433433

434434
vips_error_freeze();
435-
result = parse_matrix_header(line,
436-
&width, &height, &scale, &offset);
435+
result = parse_matrix_header(line, &width, &height, &scale, &offset);
437436
vips_error_thaw();
438437

439438
return result == 0;

0 commit comments

Comments
 (0)