Skip to content

Instantly share code, notes, and snippets.

@creationix

creationix/huh.c Secret

Created April 29, 2012 02:10
Show Gist options
  • Save creationix/a10e81a4d4cff32e2093 to your computer and use it in GitHub Desktop.
Save creationix/a10e81a4d4cff32e2093 to your computer and use it in GitHub Desktop.
size_t chunk_len;
char *chunk;
int offset;
size_t length;
size_t nparsed;
if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, vp), "Sii", &str, &offset, &length)) {
return JS_FALSE;
}
chunk_len = JS_GetStringEncodingLength(cx, str);
chunk = (char*)malloc(chunk_len);
chunk_len = JS_EncodeStringToBuffer(str, chunk, chunk_len);
this = JS_THIS_OBJECT(cx, vp);
parser = (http_parser*)JS_GetInstancePrivate(cx, this, &HttpParser_class, NULL);
if (offset < 0 || offset > chunk_len) {
JS_ReportError(cx, "Offset is out of bounds");
free(chunk);
return JS_FALSE;
}
printf("length %d, chunk_len %d, offset %d\n", (int)length, (int)chunk_len, (int)offset);
printf("chunk_len - offset %d\n", (int)(chunk_len - offset));
if (length < 0) printf("length < 0\n");
if (length > (chunk_len - offset)) printf("(length > (chunk_len - offset))\n");
if ((length < 0) || (length > (chunk_len - offset))) {
JS_ReportError(cx, "Invalid length or length extends beyond end of chunk");
free(chunk);
return JS_FALSE;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment