Commit cce467ac authored by deuce's avatar deuce
Browse files

Revert js_write() and js_read() as the can be used on the whole file.

Fix memory leaks in js_write() when using uuencoding and error conditions
occur.
parent 6f378fa0
......@@ -238,7 +238,7 @@ js_read(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
if(len<0)
len=512;
if((buf=alloca(len+1))==NULL)
if((buf=malloc(len+1))==NULL)
return(JS_TRUE);
len = fread(buf,1,len,p->fp);
......@@ -257,8 +257,10 @@ js_read(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
if(p->uuencoded || p->b64encoded || p->yencoded) {
uulen=len*2;
if((uubuf=alloca(uulen))==NULL)
if((uubuf=malloc(uulen))==NULL)
free(buf);
return(JS_TRUE);
}
if(p->uuencoded)
uulen=uuencode(uubuf,uulen,buf,len);
else if(p->yencoded)
......@@ -266,12 +268,16 @@ js_read(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
else
uulen=b64_encode(uubuf,uulen,buf,len);
if(uulen>=0) {
free(buf);
buf=uubuf;
len=uulen;
}
else
free(uubuf);
}
str = JS_NewStringCopyN(cx, buf, len);
free(buf);
if(str==NULL)
return(JS_FALSE);
......@@ -928,15 +934,17 @@ js_write(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
len = JS_GetStringLength(str);
if((p->uuencoded || p->b64encoded || p->yencoded)
&& len && (uubuf=alloca(len))!=NULL) {
&& len && (uubuf=malloc(len))!=NULL) {
if(p->uuencoded)
len=uudecode(uubuf,len,cp,len);
else if(p->yencoded)
len=ydecode(uubuf,len,cp,len);
else
len=b64_decode(uubuf,len,cp,len);
if(len<0)
if(len<0) {
free(uubuf);
return(JS_TRUE);
}
cp=uubuf;
}
......@@ -945,8 +953,10 @@ js_write(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
tlen=len;
if(argc>1) {
if(!JS_ValueToInt32(cx,argv[1],(int32*)&tlen))
if(!JS_ValueToInt32(cx,argv[1],(int32*)&tlen)) {
FREE_AND_NULL(uubuf);
return(JS_FALSE);
}
if(len>tlen)
len=tlen;
}
......@@ -954,17 +964,21 @@ js_write(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
if(fwrite(cp,1,len,p->fp)==(size_t)len) {
if(tlen>len) {
len=tlen-len;
if((cp=alloca(len))==NULL) {
dbprintf(TRUE, p, "alloca failure of %u bytes", len);
if((cp=malloc(len))==NULL) {
FREE_AND_NULL(uubuf);
dbprintf(TRUE, p, "malloc failure of %u bytes", len);
return(JS_TRUE);
}
memset(cp,p->etx,len);
fwrite(cp,1,len,p->fp);
free(cp);
}
dbprintf(FALSE, p, "wrote %u bytes",tlen);
*rval = JSVAL_TRUE;
} else
dbprintf(TRUE, p, "write of %u bytes failed",len);
FREE_AND_NULL(uubuf);
return(JS_TRUE);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment