katopz / jsc3d

Automatically exported from code.google.com/p/jsc3d
0 stars 1 forks source link

Infinite loop if loading OBJ with unclean face definitions #4

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
I exported a 3D model from Blender to OBJ format. But the OBJ file seems to 
contain some faces with only 2 vertices (Reason unknown). This crashes the 
jsc3d viewer. There is no problem opening the file with Blender.

What is the expected output? What do you see instead?
I expected the "illegal" faces would be ignored. Instead, the jsc3d library 
runs into infinite loop.

What version of the product are you using? On what operating system?
jsc3d 0.7.2,
WinXP, Win7

Please provide any additional information below.
I debugged the library and found the reason for the infinite loop in several 
do-while-statements, which test for the sequence end "-1" in the index buffer 
(ibuf[j++] != -1). But if a sequence at the end of the buffer contains only 2 
elements, this test clause can never become true.

Anyway, I suggest a very easy fix: Just ignore faces with less than 3 elements 
when parsing the file.
Here's the code:

=== Code for "JSC3D.ObjLoader.prototype.parseObj" ===

case 'f':
    if(tokens.length >= 4) {        // Add this condition
        for(var j=1; j<tokens.length; j++) {
            var refs = tokens[j].split('/');
            curMesh.indexBuffer.push( parseInt(refs[0]) - 1 );
            if(refs.length > 1 && refs[1] != '') {
                if(!curMesh.texCoordIndexBuffer)
                    curMesh.texCoordIndexBuffer = [];
                curMesh.texCoordIndexBuffer.push( parseInt(refs[1]) - 1 );
            }
        }

        curMesh.indexBuffer.push(-1);
        if(curMesh.texCoordIndexBuffer)
            curMesh.texCoordIndexBuffer.push(-1);
    }
    break;

=== End of code ===

Original issue reported on code.google.com by atan...@gmail.com on 29 Apr 2012 at 3:16

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
That's too bad. 
The current OBJ parsing routine only uses a 'split' method for extracting 
tokens and does not have much defensive code. I'm just rewriting this part 
using regular expression and dealing with more errors. All will be in next 
update.
By the way, could you please upload the OBJ file mentioned in this issue in the 
attachment? 
Thanks for the report, Atanamo!

Original comment by Humu2...@gmail.com on 7 May 2012 at 1:48

GoogleCodeExporter commented 9 years ago
Nice :-)

Here's the model.
A big "malformed" part is at the end of the file.

Original comment by atan...@gmail.com on 7 May 2012 at 10:18

Attachments: