Skip to content
Snippets Groups Projects
Commit a0a96efc authored by deuce's avatar deuce
Browse files

Use alloca() instead of malloc()/free() where possible.

NOTE: js_sendfile() should be using the xpdev socksendfile() function...
      The way it's currently working is just plain insanity.
parent 2f5fbda1
No related branches found
No related tags found
No related merge requests found
...@@ -456,6 +456,7 @@ js_sendfile(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) ...@@ -456,6 +456,7 @@ js_sendfile(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
return(JS_TRUE); return(JS_TRUE);
len=filelength(file); len=filelength(file);
/* TODO: Probobly too big for alloca()... also, this is insane. */
if((buf=malloc(len))==NULL) { if((buf=malloc(len))==NULL) {
close(file); close(file);
return(JS_TRUE); return(JS_TRUE);
...@@ -552,14 +553,13 @@ js_recv(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) ...@@ -552,14 +553,13 @@ js_recv(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
if(argc) if(argc)
JS_ValueToInt32(cx,argv[0],&len); JS_ValueToInt32(cx,argv[0],&len);
if((buf=(char*)malloc(len+1))==NULL) { if((buf=(char*)alloca(len+1))==NULL) {
JS_ReportError(cx,"Error allocating %u bytes",len+1); JS_ReportError(cx,"Error allocating %u bytes",len+1);
return(JS_FALSE); return(JS_FALSE);
} }
len = recv(p->sock,buf,len,0); len = recv(p->sock,buf,len,0);
if(len<0) { if(len<0) {
free(buf);
p->last_error=ERROR_VALUE; p->last_error=ERROR_VALUE;
*rval = JSVAL_NULL; *rval = JSVAL_NULL;
return(JS_TRUE); return(JS_TRUE);
...@@ -567,7 +567,6 @@ js_recv(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) ...@@ -567,7 +567,6 @@ js_recv(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
buf[len]=0; buf[len]=0;
str = JS_NewStringCopyN(cx, buf, len); str = JS_NewStringCopyN(cx, buf, len);
free(buf);
if(str==NULL) if(str==NULL)
return(JS_FALSE); return(JS_FALSE);
...@@ -646,21 +645,19 @@ js_recvfrom(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) ...@@ -646,21 +645,19 @@ js_recvfrom(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
} else { /* String Data */ } else { /* String Data */
if((buf=(char*)malloc(len+1))==NULL) { if((buf=(char*)alloca(len+1))==NULL) {
JS_ReportError(cx,"Error allocating %u bytes",len+1); JS_ReportError(cx,"Error allocating %u bytes",len+1);
return(JS_FALSE); return(JS_FALSE);
} }
len = recvfrom(p->sock,buf,len,0,(SOCKADDR*)&addr,&addrlen); len = recvfrom(p->sock,buf,len,0,(SOCKADDR*)&addr,&addrlen);
if(len<0) { if(len<0) {
free(buf);
p->last_error=ERROR_VALUE; p->last_error=ERROR_VALUE;
return(JS_TRUE); return(JS_TRUE);
} }
buf[len]=0; buf[len]=0;
str = JS_NewStringCopyN(cx, buf, len); str = JS_NewStringCopyN(cx, buf, len);
free(buf);
if(str==NULL) if(str==NULL)
return(JS_FALSE); return(JS_FALSE);
...@@ -717,13 +714,12 @@ js_peek(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) ...@@ -717,13 +714,12 @@ js_peek(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
if(argc) if(argc)
JS_ValueToInt32(cx,argv[0],&len); JS_ValueToInt32(cx,argv[0],&len);
if((buf=(char*)malloc(len+1))==NULL) { if((buf=(char*)alloca(len+1))==NULL) {
JS_ReportError(cx,"Error allocating %u bytes",len+1); JS_ReportError(cx,"Error allocating %u bytes",len+1);
return(JS_FALSE); return(JS_FALSE);
} }
len = recv(p->sock,buf,len,MSG_PEEK); len = recv(p->sock,buf,len,MSG_PEEK);
if(len<0) { if(len<0) {
free(buf);
p->last_error=ERROR_VALUE; p->last_error=ERROR_VALUE;
*rval = JSVAL_NULL; *rval = JSVAL_NULL;
return(JS_TRUE); return(JS_TRUE);
...@@ -731,7 +727,6 @@ js_peek(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) ...@@ -731,7 +727,6 @@ js_peek(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
buf[len]=0; buf[len]=0;
str = JS_NewStringCopyN(cx, buf, len); str = JS_NewStringCopyN(cx, buf, len);
free(buf);
if(str==NULL) if(str==NULL)
return(JS_FALSE); return(JS_FALSE);
...@@ -763,7 +758,7 @@ js_recvline(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) ...@@ -763,7 +758,7 @@ js_recvline(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
if(argc) if(argc)
JS_ValueToInt32(cx,argv[0],&len); JS_ValueToInt32(cx,argv[0],&len);
if((buf=(char*)malloc(len+1))==NULL) { if((buf=(char*)alloca(len+1))==NULL) {
JS_ReportError(cx,"Error allocating %u bytes",len+1); JS_ReportError(cx,"Error allocating %u bytes",len+1);
return(JS_FALSE); return(JS_FALSE);
} }
...@@ -781,7 +776,6 @@ js_recvline(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) ...@@ -781,7 +776,6 @@ js_recvline(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
if(!rd) { if(!rd) {
if(time(NULL)-start>timeout) { if(time(NULL)-start>timeout) {
free(buf);
dbprintf(FALSE, p, "recvline timeout (received: %d)",i); dbprintf(FALSE, p, "recvline timeout (received: %d)",i);
*rval = JSVAL_NULL; *rval = JSVAL_NULL;
return(JS_TRUE); /* time-out */ return(JS_TRUE); /* time-out */
...@@ -805,7 +799,6 @@ js_recvline(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) ...@@ -805,7 +799,6 @@ js_recvline(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
buf[i]=0; buf[i]=0;
str = JS_NewStringCopyZ(cx, buf); str = JS_NewStringCopyZ(cx, buf);
free(buf);
if(str==NULL) if(str==NULL)
return(JS_FALSE); return(JS_FALSE);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment