bf78d2
diff -urNp old/plug-ins/file-fli/fli.c new/plug-ins/file-fli/fli.c
bf78d2
--- old/plug-ins/file-fli/fli.c	2018-01-04 12:19:54.714139464 +0100
bf78d2
+++ new/plug-ins/file-fli/fli.c	2018-01-04 12:34:18.568323629 +0100
bf78d2
@@ -25,6 +25,8 @@
bf78d2
 
bf78d2
 #include "config.h"
bf78d2
 
bf78d2
+#include <glib/gstdio.h>
bf78d2
+
bf78d2
 #include <string.h>
bf78d2
 #include <stdio.h>
bf78d2
 
bf78d2
@@ -461,23 +463,27 @@ void fli_read_brun(FILE *f, s_fli_header
bf78d2
 	unsigned short yc;
bf78d2
 	unsigned char *pos;
bf78d2
 	for (yc=0; yc < fli_header->height; yc++) {
bf78d2
-		unsigned short xc, pc, pcnt;
bf78d2
+		unsigned short pc, pcnt;
bf78d2
+                size_t n, xc;
bf78d2
 		pc=fli_read_char(f);
bf78d2
 		xc=0;
bf78d2
 		pos=framebuf+(fli_header->width * yc);
bf78d2
+                n=(size_t)fli_header->width * (fli_header->height-yc);
bf78d2
 		for (pcnt=pc; pcnt>0; pcnt--) {
bf78d2
 			unsigned short ps;
bf78d2
 			ps=fli_read_char(f);
bf78d2
 			if (ps & 0x80) {
bf78d2
 				unsigned short len;
bf78d2
-				for (len=-(signed char)ps; len>0; len--) {
bf78d2
+				for (len=-(signed char)ps; len>0 && xc
bf78d2
 					pos[xc++]=fli_read_char(f);
bf78d2
 				}
bf78d2
 			} else {
bf78d2
 				unsigned char val;
bf78d2
+                                size_t len;
bf78d2
+                                len=MIN(n-xc,ps);
bf78d2
 				val=fli_read_char(f);
bf78d2
-				memset(&(pos[xc]), val, ps);
bf78d2
-				xc+=ps;
bf78d2
+				memset(&(pos[xc]), val, len);
bf78d2
+				xc+=len;
bf78d2
 			}
bf78d2
 		}
bf78d2
 	}
bf78d2
@@ -564,25 +570,34 @@ void fli_read_lc(FILE *f, s_fli_header *
bf78d2
 	memcpy(framebuf, old_framebuf, fli_header->width * fli_header->height);
bf78d2
 	firstline = fli_read_short(f);
bf78d2
 	numline = fli_read_short(f);
bf78d2
+        if (numline > fli_header->height || fli_header->height-numline < firstline)
bf78d2
+                return;
bf78d2
+
bf78d2
 	for (yc=0; yc < numline; yc++) {
bf78d2
-		unsigned short xc, pc, pcnt;
bf78d2
+		unsigned short pc, pcnt;
bf78d2
+                size_t n, xc;
bf78d2
 		pc=fli_read_char(f);
bf78d2
 		xc=0;
bf78d2
 		pos=framebuf+(fli_header->width * (firstline+yc));
bf78d2
+                n=(size_t)fli_header->width * (fli_header->height-firstline-yc);
bf78d2
 		for (pcnt=pc; pcnt>0; pcnt--) {
bf78d2
 			unsigned short ps,skip;
bf78d2
 			skip=fli_read_char(f);
bf78d2
 			ps=fli_read_char(f);
bf78d2
-			xc+=skip;
bf78d2
+			xc+=MIN(n-xc,skip);
bf78d2
 			if (ps & 0x80) {
bf78d2
 				unsigned char val;
bf78d2
+                                size_t len;
bf78d2
 				ps=-(signed char)ps;
bf78d2
 				val=fli_read_char(f);
bf78d2
-				memset(&(pos[xc]), val, ps);
bf78d2
-				xc+=ps;
bf78d2
+                                len=MIN(n-xc,ps);
bf78d2
+				memset(&(pos[xc]), val, len);
bf78d2
+				xc+=len;
bf78d2
 			} else {
bf78d2
-				fread(&(pos[xc]), ps, 1, f);
bf78d2
-				xc+=ps;
bf78d2
+                                size_t len;
bf78d2
+                                len=MIN(n-xc,ps);
bf78d2
+				fread(&(pos[xc]), len, 1, f);
bf78d2
+				xc+=len;
bf78d2
 			}
bf78d2
 		}
bf78d2
 	}
bf78d2
@@ -689,7 +704,8 @@ void fli_read_lc_2(FILE *f, s_fli_header
bf78d2
 	yc=0;
bf78d2
 	numline = fli_read_short(f);
bf78d2
 	for (lc=0; lc < numline; lc++) {
bf78d2
-		unsigned short xc, pc, pcnt, lpf, lpn;
bf78d2
+		unsigned short pc, pcnt, lpf, lpn;
bf78d2
+                size_t n, xc;
bf78d2
 		pc=fli_read_short(f);
bf78d2
 		lpf=0; lpn=0;
bf78d2
 		while (pc & 0x8000) {
bf78d2
@@ -700,26 +716,30 @@ void fli_read_lc_2(FILE *f, s_fli_header
bf78d2
 			}
bf78d2
 			pc=fli_read_short(f);
bf78d2
 		}
bf78d2
+                yc=MIN(yc, fli_header->height);
bf78d2
 		xc=0;
bf78d2
 		pos=framebuf+(fli_header->width * yc);
bf78d2
+                n=(size_t)fli_header->width * (fli_header->height-yc);
bf78d2
 		for (pcnt=pc; pcnt>0; pcnt--) {
bf78d2
 			unsigned short ps,skip;
bf78d2
 			skip=fli_read_char(f);
bf78d2
 			ps=fli_read_char(f);
bf78d2
-			xc+=skip;
bf78d2
+			xc+=MIN(n-xc,skip);
bf78d2
 			if (ps & 0x80) {
bf78d2
 				unsigned char v1,v2;
bf78d2
 				ps=-(signed char)ps;
bf78d2
 				v1=fli_read_char(f);
bf78d2
 				v2=fli_read_char(f);
bf78d2
-				while (ps>0) {
bf78d2
+				while (ps>0 && xc+1
bf78d2
 					pos[xc++]=v1;
bf78d2
 					pos[xc++]=v2;
bf78d2
 					ps--;
bf78d2
 				}
bf78d2
 			} else {
bf78d2
-				fread(&(pos[xc]), ps, 2, f);
bf78d2
-				xc+=ps << 1;
bf78d2
+                                size_t len;
bf78d2
+                                len=MIN((n-xc)/2,ps);
bf78d2
+				fread(&(pos[xc]), len, 2, f);
bf78d2
+				xc+=len << 1;
bf78d2
 			}
bf78d2
 		}
bf78d2
 		if (lpf) pos[xc]=lpn;