Closed Bug 79485 Opened 23 years ago Closed 9 years ago

JSSourceHandler called with wrong line length parameter

Categories

(Core :: JavaScript Engine, defect, P3)

defect

Tracking

()

RESOLVED WORKSFORME
mozilla1.2alpha

People

(Reporter: brendan, Assigned: rginda)

References

()

Details

(Keywords: js1.5)

Attachments

(2 files)

See news://news.mozilla.org/9d95kt%24e36%241%40news1.wdf.sap-ag.de while it
lasts.  Better URL welcome.

Patch coming up, also picks some unrelated nits in jsscan.c.

/be
Status: NEW → ASSIGNED
Keywords: js1.5, mozilla0.9.1
Priority: -- → P1
Target Milestone: --- → mozilla0.9.1
I left the listener call still passing ts->userbuf.ptr, but the len actual arg
it passes counts the canonical-newline-terminated characters in ts->linebuf.  So
for long userbuf lines, multiple listener calls are possible.  And for lines
terminated by \r\n in userbuf, len will count only one terminator character.  Is
this wrong?  I could farble len a bit to compute the user line length (if that
line fits in linebuf).

Comments from you debugger guys welcome.

/be
Wait, I was on crack: userbuf contains len chars, and if the user-input line
ends with \r\n, that's counted by len and included in the buffer prefix seen by
ts->listener.  So I'm ready for r=rginda and sr=jband.

/be
Keywords: approval, patch, review
We're passing userbuf.ptr to the listener via jschar *, instead of const jschar
*.  Other than that, r=rginda.
OK. I'm stupid. I'm still trying to figure out what was wrong in the first 
place. I don't understand the assertion that "JS_CompileScript calls the 
callback for each line". This code was originally intended to send chunks of 
text to the listener without regard for sending each line separately. I can't 
see that it *does* send line by line. 

I just now wrote a test case that shows the old code did not seem to do exactly 
what was said above. But still did not do the right thing. My test also shows 
brendan's patch not working right...

Here's the test (easy to build if saved as js.c):

/**************************************************************************/
#include "jsapi.h"
#include "jsdbgapi.h"
#include <stdio.h>

void handler(const char *filename, uintN lineno, 
             jschar *str, size_t length, 
             void **listenerTSData, void *closure)
{
    size_t i;

    printf("handler called for [%s,%d] with length: %d and text: \n", 
           filename,lineno, length);
    for(i = 0; i < length; i++)
        putchar((char)str[i]);
    
    putchar('\n');
}             

static JSBool
global_resolve(JSContext *cx, JSObject *obj, jsval id)
{
    JSBool resolved;
    return JS_ResolveStandardClass(cx, obj, id, &resolved);
}

static JSClass global_class = {
    "global", 0,
    JS_PropertyStub,  JS_PropertyStub,  JS_PropertyStub,  JS_PropertyStub,
    JS_EnumerateStub, global_resolve,   JS_ConvertStub,   JS_FinalizeStub
};

int main()
{
    const char buffer1[] = "Some\nmultiline text\nthat covers many\nlines.\n";       
    const char buffer2[] = "Some\nmultiline text\nthat covers many\nlines.";       
    JSRuntime *rt;
    JSContext *cx;
    JSObject *glob;
    
    rt = JS_NewRuntime(8L * 1024L * 1024L);
    cx = JS_NewContext(rt, 4048);

    glob = JS_NewObject(cx, &global_class, NULL, NULL);
    JS_SetGlobalObject(cx, glob);

    JS_SetSourceHandler(rt, handler, NULL);

    JS_CompileScript(cx, glob, buffer1, sizeof(buffer1)-1, "buffer1", 1);
    printf("----------------------------------------------------------\n");
    JS_CompileScript(cx, glob, buffer2, sizeof(buffer2)-1, "buffer2", 1);

    JS_DestroyContext(cx);
    JS_DestroyRuntime(rt);
    JS_ShutDown();
    return 0;
}
/**************************************************************************/

The old code outputs:

handler called for [buffer1,1] with length: 44 and text:
Some
multiline text
that covers many
lines.

handler called for [buffer1,2] with length: 39 and text:
multiline text
that covers many
lines.

----------------------------------------------------------
handler called for [buffer2,1] with length: 43 and text:
Some
multiline text
that covers many
lines.
handler called for [buffer2,2] with length: 38 and text:
multiline text
that covers many
lines.

With brendan's patch I see:

handler called for [buffer1,1] with length: 5 and text:
Some

handler called for [buffer1,2] with length: 15 and text:
multiline text

----------------------------------------------------------
handler called for [buffer2,1] with length: 5 and text:
Some

handler called for [buffer2,2] with length: 15 and text:
multiline text
Flail smarter, not harder.

/be
Target Milestone: mozilla0.9.1 → mozilla0.9.2
Target Milestone: mozilla0.9.2 → mozilla0.9.3
*** Bug 91909 has been marked as a duplicate of this bug. ***
See also bug 91909 for additional comments
This little bug should get fixed soon, or I will renounce it.

/be
Target Milestone: mozilla0.9.3 → mozilla0.9.4
Not holding up 0.9.4 for this, but I'll work up a better patch soon.

/be
Target Milestone: mozilla0.9.4 → mozilla0.9.5
rginda, is this still bugging you?  I've been busy elsewhere, and would like to
push it out even further, or give it away if someone else will fix it right
(I've tried and failed once already).

/be
Target Milestone: mozilla0.9.5 → mozilla0.9.6
I don't actually use the source handler, this bug report came from a newsgroup
posting by Olaf Meincke, <olaf.meincke@sap.com>.
I applied the proposed bug fix and since haven't encountered any problems with 
the source handler. From my point of view the problem is fixed.
My posting may not have been very clear. My point was that brendan's fix (while 
it may work better for you) is nonetheless broken and should not be checked in. 
Still, we now have code that is headed in the right direction and a half-assed 
test framework available for use by whoever comes up to bat next.
Priority: P1 → P3
Who wants to bat next?  Please swing away, reassign to yourself and cc: me.

/be
Keywords: mozilla0.9.6mozilla1.0
Target Milestone: mozilla0.9.6 → mozilla0.9.7
I'm a terrible owner for this bug.  Rob, you wanna take a whack at it?

/be
Target Milestone: mozilla0.9.7 → mozilla0.9.8
taking
Assignee: brendan → rginda
Status: ASSIGNED → NEW
Status: NEW → ASSIGNED
Target Milestone: mozilla0.9.8 → mozilla1.0
Moving Netscape owned 0.9.9 and 1.0 bugs that don't have an nsbeta1, nsbeta1+,
topembed, topembed+, Mozilla0.9.9+ or Mozilla1.0+ keyword.  Please send any
questions or feedback about this to adt@netscape.com.  You can search for
"Moving bugs not scheduled for a project" to quickly delete this bugmail.
Target Milestone: mozilla1.0 → mozilla1.2
Target Milestone: mozilla1.2 → mozilla1.0
Pushing out.  Venkman doesn't need this, I don't see how I'm going to find time
to get to it before 1.0.
Target Milestone: mozilla1.0 → mozilla1.2
Blocks: 149801
QA Contact: pschwartau → general
No longer an issue, bug 588648 (http://hg.mozilla.org/mozilla-central/rev/2b9d805d77a1) removed that code from GetChar.
Status: ASSIGNED → RESOLVED
Closed: 9 years ago
Resolution: --- → WORKSFORME
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: