Skip to content

Commit 0964d60

Browse files
committed
fix some int32 limits on windows
write() on windows uses int for byte counts, leading to errors with very wide (more than 1.5m pixels wide) images
1 parent ee482ca commit 0964d60

File tree

8 files changed

+33
-50
lines changed

8 files changed

+33
-50
lines changed

libvips/deprecated/package.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ static int
323323
getext_vec(im_object *argv)
324324
{
325325
void **out = (void **) &argv[1];
326-
int size;
326+
size_t size;
327327

328328
/* void/char confusion is fine.
329329
*/

libvips/foreign/rawsave.c

+9-18
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,12 @@ vips_foreign_save_raw_write(VipsRegion *region, VipsRect *area, void *a)
9393
{
9494
VipsForeignSave *save = (VipsForeignSave *) a;
9595
VipsForeignSaveRaw *raw = (VipsForeignSaveRaw *) a;
96-
int i;
9796

98-
for (i = 0; i < area->height; i++) {
99-
VipsPel *p =
100-
VIPS_REGION_ADDR(region, area->left, area->top + i);
101-
102-
if (vips__write(raw->fd, p,
103-
VIPS_IMAGE_SIZEOF_PEL(save->in) * area->width))
97+
for (int i = 0; i < area->height; i++)
98+
if (vips__write(raw->fd,
99+
VIPS_REGION_ADDR(region, area->left, area->top + i),
100+
VIPS_IMAGE_SIZEOF_PEL(save->in) * area->width))
104101
return -1;
105-
}
106102

107103
return 0;
108104
}
@@ -207,16 +203,12 @@ vips_foreign_save_raw_fd_write(VipsRegion *region, VipsRect *area, void *a)
207203
{
208204
VipsForeignSave *save = (VipsForeignSave *) a;
209205
VipsForeignSaveRawFd *fd = (VipsForeignSaveRawFd *) a;
210-
int i;
211-
212-
for (i = 0; i < area->height; i++) {
213-
VipsPel *p =
214-
VIPS_REGION_ADDR(region, area->left, area->top + i);
215206

216-
if (vips__write(fd->fd, p,
217-
VIPS_IMAGE_SIZEOF_PEL(save->in) * area->width))
207+
for (int i = 0; i < area->height; i++)
208+
if (vips__write(fd->fd,
209+
VIPS_REGION_ADDR(region, area->left, area->top + i),
210+
VIPS_IMAGE_SIZEOF_PEL(save->in) * area->width))
218211
return -1;
219-
}
220212

221213
return 0;
222214
}
@@ -231,8 +223,7 @@ vips_foreign_save_raw_fd_build(VipsObject *object)
231223
return -1;
232224

233225
if (vips_image_pio_input(save->in) ||
234-
vips_sink_disc(save->in,
235-
vips_foreign_save_raw_fd_write, fd))
226+
vips_sink_disc(save->in, vips_foreign_save_raw_fd_write, fd))
236227
return -1;
237228

238229
return 0;

libvips/include/vips/internal.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,11 @@ int vips__has_extension_block(VipsImage *im);
223223
/* TODO(kleisauke): VIPS_API is required by vipsheader.
224224
*/
225225
VIPS_API
226-
void *vips__read_extension_block(VipsImage *im, int *size);
226+
void *vips__read_extension_block(VipsImage *im, size_t *size);
227227
/* TODO(kleisauke): VIPS_API is required by vipsedit.
228228
*/
229229
VIPS_API
230-
int vips__write_extension_block(VipsImage *im, void *buf, int size);
230+
int vips__write_extension_block(VipsImage *im, void *buf, size_t size);
231231
int vips__writehist(VipsImage *image);
232232
/* TODO(kleisauke): VIPS_API is required by vipsedit.
233233
*/

libvips/include/vips/util.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,7 @@ int vips__file_write(void *data, size_t size, size_t nmemb, FILE *stream);
338338
/* TODO(kleisauke): VIPS_API is required by the magick module.
339339
*/
340340
VIPS_API
341-
gint64 vips__get_bytes(const char *filename,
342-
unsigned char buf[], gint64 len);
341+
gint64 vips__get_bytes(const char *filename, unsigned char buf[], gint64 len);
343342
int vips__fgetc(FILE *fp);
344343

345344
GValue *vips__gvalue_ref_string_new(const char *text);

libvips/iofuncs/image.c

+4-7
Original file line numberDiff line numberDiff line change
@@ -3203,7 +3203,7 @@ vips_image_write_prepare(VipsImage *image)
32033203
int
32043204
vips_image_write_line(VipsImage *image, int ypos, VipsPel *linebuffer)
32053205
{
3206-
int linesize = VIPS_IMAGE_SIZEOF_LINE(image);
3206+
guint64 linesize = VIPS_IMAGE_SIZEOF_LINE(image);
32073207

32083208
/* Is this the start of eval?
32093209
*/
@@ -3224,8 +3224,7 @@ vips_image_write_line(VipsImage *image, int ypos, VipsPel *linebuffer)
32243224
switch (image->dtype) {
32253225
case VIPS_IMAGE_SETBUF:
32263226
case VIPS_IMAGE_SETBUF_FOREIGN:
3227-
memcpy(VIPS_IMAGE_ADDR(image, 0, ypos),
3228-
linebuffer, linesize);
3227+
memcpy(VIPS_IMAGE_ADDR(image, 0, ypos), linebuffer, linesize);
32293228
break;
32303229

32313230
case VIPS_IMAGE_OPENOUT:
@@ -3236,10 +3235,8 @@ vips_image_write_line(VipsImage *image, int ypos, VipsPel *linebuffer)
32363235
break;
32373236

32383237
default:
3239-
vips_error("VipsImage",
3240-
_("unable to output to a %s image"),
3241-
vips_enum_string(VIPS_TYPE_IMAGE_TYPE,
3242-
image->dtype));
3238+
vips_error("VipsImage", _("unable to output to a %s image"),
3239+
vips_enum_string(VIPS_TYPE_IMAGE_TYPE, image->dtype));
32433240
return -1;
32443241
}
32453242

libvips/iofuncs/util.c

+10-12
Original file line numberDiff line numberDiff line change
@@ -558,11 +558,13 @@ int
558558
vips__write(int fd, const void *buf, size_t count)
559559
{
560560
do {
561-
size_t nwritten = write(fd, buf, count);
561+
// write() uses int not ssize_t on windows, so we need to chunk
562+
// ... max 1gb, why not
563+
int chunk_size = VIPS_MIN(1024 * 1024 * 1024, count);
564+
ssize_t nwritten = write(fd, buf, chunk_size);
562565

563-
if (nwritten == (size_t) -1) {
564-
vips_error_system(errno, "vips__write",
565-
"%s", _("write failed"));
566+
if (nwritten == (ssize_t) -1) {
567+
vips_error_system(errno, "vips__write", "%s", _("write failed"));
566568
return -1;
567569
}
568570

@@ -747,8 +749,7 @@ vips__file_read(FILE *fp, const char *filename, size_t *length_out)
747749
if (len > 1024 * 1024 * 1024) {
748750
/* Over a gb? Seems crazy!
749751
*/
750-
vips_error("vips__file_read",
751-
_("\"%s\" too long"), filename);
752+
vips_error("vips__file_read", _("\"%s\" too long"), filename);
752753
return NULL;
753754
}
754755

@@ -770,17 +771,15 @@ vips__file_read(FILE *fp, const char *filename, size_t *length_out)
770771
if (size > 1024 * 1024 * 1024 ||
771772
!(str2 = realloc(str, size))) {
772773
free(str);
773-
vips_error("vips__file_read",
774-
"%s", _("out of memory"));
774+
vips_error("vips__file_read", "%s", _("out of memory"));
775775
return NULL;
776776
}
777777
str = str2;
778778

779779
/* -1 to allow space for an extra NULL we add later.
780780
*/
781781
read = fread(str + len, sizeof(char),
782-
(size - len - 1) / sizeof(char),
783-
fp);
782+
(size - len - 1) / sizeof(char), fp);
784783
len += read;
785784
} while (!feof(fp));
786785

@@ -798,8 +797,7 @@ vips__file_read(FILE *fp, const char *filename, size_t *length_out)
798797
if (read != (size_t) len) {
799798
g_free(str);
800799
vips_error("vips__file_read",
801-
_("error reading from file \"%s\""),
802-
filename);
800+
_("error reading from file \"%s\""), filename);
803801
return NULL;
804802
}
805803
}

libvips/iofuncs/vips.c

+4-5
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ vips__has_extension_block(VipsImage *im)
506506
/* Read everything after the pixels into memory.
507507
*/
508508
void *
509-
vips__read_extension_block(VipsImage *im, int *size)
509+
vips__read_extension_block(VipsImage *im, size_t *size)
510510
{
511511
gint64 psize;
512512
void *buf;
@@ -515,8 +515,7 @@ vips__read_extension_block(VipsImage *im, int *size)
515515
g_assert(im->file_length > 0);
516516
if (im->file_length - psize > 100 * 1024 * 1024) {
517517
vips_error("VipsImage",
518-
"%s", _("more than 100 megabytes of XML? "
519-
"sufferin' succotash!"));
518+
"%s", _("more than 100 megabytes of XML? sufferin' succotash!"));
520519
return NULL;
521520
}
522521
if (im->file_length - psize == 0)
@@ -773,7 +772,7 @@ readhist(VipsImage *im)
773772
}
774773

775774
int
776-
vips__write_extension_block(VipsImage *im, void *buf, int size)
775+
vips__write_extension_block(VipsImage *im, void *buf, size_t size)
777776
{
778777
gint64 length;
779778
gint64 psize;
@@ -793,7 +792,7 @@ vips__write_extension_block(VipsImage *im, void *buf, int size)
793792
return -1;
794793

795794
#ifdef DEBUG
796-
printf("vips__write_extension_block: written %d bytes of XML to %s\n",
795+
printf("vips__write_extension_block: written %zd bytes of XML to %s\n",
797796
size, im->filename);
798797
#endif /*DEBUG*/
799798

tools/vipsheader.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,9 @@ print_header(VipsImage *image, gboolean many)
143143
else if (strcmp(main_option_field, "getext") == 0) {
144144
if (vips__has_extension_block(image)) {
145145
void *buf;
146-
int size;
146+
size_t size;
147147

148-
if (!(buf =
149-
vips__read_extension_block(image, &size)))
148+
if (!(buf = vips__read_extension_block(image, &size)))
150149
return -1;
151150
printf("%s", (char *) buf);
152151
g_free(buf);

0 commit comments

Comments
 (0)