Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infinite loop if loading OBJ with unclean face definitions #4

Open
GoogleCodeExporter opened this issue Mar 19, 2015 · 3 comments
Open

Comments

@GoogleCodeExporter
Copy link

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
Copy link
Author

[deleted comment]

@GoogleCodeExporter
Copy link
Author

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
Copy link
Author

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:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant