From d2ac211a1070e0686a167f5473abc8ead1df6f6c Mon Sep 17 00:00:00 2001
From: deuce <>
Date: Tue, 10 Nov 2015 11:49:37 +0000
Subject: [PATCH] Fix bugs in last commit and check the return value of
 setvbuf().

---
 src/sbbs3/js_file.c | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)

diff --git a/src/sbbs3/js_file.c b/src/sbbs3/js_file.c
index 4d9ea7856b..b134529af7 100644
--- a/src/sbbs3/js_file.c
+++ b/src/sbbs3/js_file.c
@@ -2126,26 +2126,32 @@ static JSBool js_file_set(JSContext *cx, JSObject *obj, jsid id, JSBool strict,
 		case FILE_PROP_BUFSIZE:
 			if(!JS_ValueToECMAUint32(cx,*vp,&u))
 				return(JS_FALSE);
-			p->bufsize = u;
-			setvbuf(p->fp, NULL, p->bufmode, p->bufsize);
+			if (setvbuf(p->fp, NULL, p->bufmode, p->bufsize) == -1)
+				JS_ReportError(cx, "Unable to set buffer size to %u", u);
+			else
+				p->bufsize = u;
 			break;
 		case FILE_PROP_BUFMODE:
 			if(!JS_ValueToInt32(cx,*vp,&i))
 				return(JS_FALSE);
 			switch(i) {
 				case 0:
-					p->bufmode = _IONBF;
+					i = _IONBF;
 					break;
 				case 1:
-					p->bufmode = _IOLBF;
+					i = _IOLBF;
 					break;
 				case 2:
-					p->bufmode = _IOFBF;
+					i = _IOFBF;
 					break;
 				default:
-					JS_ReportError(cx,"Invalid buffer mode",WHERE);
+					JS_ReportError(cx,"Invalid buffer mode");
 					return JS_FALSE;
 			}
+			if (setvbuf(p->fp, NULL, i, p->bufsize) == -1)
+				JS_ReportError(cx, "Unable to set buffer mode to %d", p->bufmode);
+			else
+				p->bufmode = i;
 			break;
 	}
 
@@ -2369,7 +2375,7 @@ static JSBool js_file_get(JSContext *cx, JSObject *obj, jsid id, jsval *vp)
 					*vp = INT_TO_JSVAL(2);
 					break;
 				default:
-					JS_ReportError(cx,"Invalid buffer mode",WHERE);
+					JS_ReportError(cx,"Invalid buffer mode");
 					return JS_FALSE;
 			}
 			break;
-- 
GitLab