syoyo / tinyobjloader-c

Header only tiny wavefront .obj loader in pure C99
421 stars 60 forks source link

Infinite loop in hash table inserting when reading mtl file #52

Closed Sharlock93 closed 2 years ago

Sharlock93 commented 2 years ago

The hash_table_insert_value hits an infinite loop if the capacity is 10 and the start_index is 9, the (i*i) addition doesn't produce every possible index and in my case unfortunately it so happens that every index it produces were filled, here is a simple log:

|| ===============
|| start index 9 index 9
|| i : 1) new index 0
|| i : 2) new index 3
|| i : 3) new index 8
|| i : 4) new index 5
|| i : 5) new index 4
|| i : 6) new index 5
|| i : 7) new index 8
|| i : 8) new index 3
|| i : 9) new index 0
|| ===============

here are the files to reproduce this: car.zip

changing i*i to a simple i fixes this.

No clue if it has been reported before.

syoyo commented 2 years ago

Reading car.obj with viewer https://github.com/syoyo/tinyobjloader-c/tree/master/examples/viewer does not go into infinite loop.

Please post reproducible code and procedure.

Sharlock93 commented 2 years ago

Please post reproducible code and procedure.

this is my main and nothing else:

int main() {

    tinyobj_attrib_t car_mesh = {0};
    tinyobj_shape_t *car_shapes = NULL;
    u64 car_shape_count;
    tinyobj_material_t *car_materials = NULL;
    u64 car_material_count;
    tinyobj_parse_obj(&car_mesh, &car_shapes, &car_shape_count, &car_materials, &car_material_count, "../assets/cw1/car.obj", get_file_data, NULL, TINYOBJ_FLAG_TRIANGULATE);

    return 0;
}

get_file_datais the same as in viewer.c, I'm on using Windows and Visual Studio if that makes any different.

Reading car.obj with viewer https://github.com/syoyo/tinyobjloader-c/tree/master/examples/viewer does not go into infinite loop.

the problem happened with the material, although pedantic to ask but could you check and see if the material file has been read? I don't have access to a linux machine to test viewer.c immediately.

syoyo commented 2 years ago

You can use WSL2. And viewer works well without issue on my WSL2 environment.

Screenshot 2022-05-21 032742

Anyway, hashtable code is provided by a contributor, not me. So I don't know how reliable it is. You can contribute more reliable and robust hashtable implementation!

syoyo commented 2 years ago

BTW you should use size_t as requested by the function signature

https://github.com/syoyo/tinyobjloader-c/blob/5cd6dfd25c28b426e80ce5150248911b7acba97b/tinyobj_loader_c.h#L118

, not (your own?) u64.

u64 car_shape_count;