f3d: PLY point clouds without a mesh crash the program

Describe the bug When trying to open a PLY that is just a colored point cloud, F3D says there’s no faces, but also crashes.

This is generated from RTAB-Map’s default export options. RTAB-Map’s viewer is also VTK so I believe this should work.

To Reproduce Steps to reproduce the behavior:

  1. Open the file f3d dog.ply
  2. Observe crash

Expected behavior A graceful exit at least (possibly supporting this type of PLY)

System Information:

  • OS: Arch Linux
  • GPU and GPU driver: 780M AMDGPU

F3D Information

F3D 3.0.0

F3D - A fast and minimalist 3D viewer
Version: 3.0.0-RC3-9-g8d72f2d0.
Build date: 2025-01-15 15:07:23.
Build system: Linux 64-bits.
Compiler: GNU 14.2.1.
Module ImGui: ON.
Module OpenEXR: ON.
Module Raytracing: ON.
VTK version: 9.4.1.
Copyright (C) 2019-2021 Kitware SAS.
Copyright (C) 2021-2024 Michael Migliore, Mathieu Westphal.
License BSD-3-Clause.

Additional context

[parker@parker-framework RTAB-Map]$ f3d dog.ply 
Warning:  Can't find property 'vertex_indices' in element 'face'
free(): double free detected in tcache 2
Aborted (core dumped)

Image

File

dog.zip

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 19 (16 by maintainers)

Most upvoted comments

Overall it is not good to see C-style memory management in C++ code, but I guess there are reasons for it 😃

That code is OLD.

Thanks for investigating!

(btw we have a discord, feel free to join if thats your thing @exbluesbreaker : https://discord.f3d.app)

The fix has been merged in VTK, Ill bump VTK in our CI

https://gitlab.kitware.com/vtk/vtk/-/merge_requests/11961

so maybe it’s not within scope for F3D to display?

It definitely is in the scope of F3D 😃

pressing Ctrl+C sometimes leaves the app unresponsive instead of closing it,

Ive seen that sometimes, with other files, i think its another bug.

Hello! So it seems I figure out the issue. ply file had those elements:

element vertex 139777- point cloud with a lot of points element face 0- no polygons (a bit uncommon for ply but could happen with like raw point cloud input) element camera 1

The issue is the absence of polygons, which means also no properties are allocated, leaving props uninitialized. As a result, free is called on an invalid pointer, causing a crash. In vtkPLY::add_property, properties are allocated, so vtkPLY::ply_close should check elem->nprops > 0 before freeing props. Adding this check prevents the crash.

// vtkPLY::add_property
...
  if (elem->nprops == 0)
    elem->props = (PlyProperty**)myalloc(sizeof(PlyProperty*));
  else
    elem->props = (PlyProperty**)realloc(elem->props, sizeof(PlyProperty*) * (elem->nprops + 1));
...

Place of crash without check:

// vtkPLY::ply_close
...
  PlyElement* elem;
  for (i = 0; i < plyfile->nelems; i++)
  {
    elem = plyfile->elems[i];
    free(elem->name);
    for (j = 0; j < elem->nprops; j++)
    {
      free(const_cast<char*>(elem->props[j]->name));
      free(elem->props[j]);
    }
    free(elem->props);//crash happens here 
    free(elem->store_prop);
    free(elem);
  }
...

I will prepare MR for VTK with fix for it. The only thing which still bothers me, I may have found another issue—after successfully displaying a PLY file, pressing Ctrl+C sometimes leaves the app unresponsive instead of closing it, requiring a manual kill. I couldn’t reproduce this with a different file.

Also here is result of display of ply file. I doesn’t look exactly like dog to me, but who knows 😃

Image

I joined, just didn’t yet talk there)

Hello!

I’m working on this problem. There are two ply element creations and deletions, but the second one involves uninitialized heap memory that later gets freed. I’ll continue tomorrow—I’ve spent some time on it. It seems there should be better initialization since the freeing code assumes all pointers are valid and doesn’t check for NULL(and pointer is not null just not initialized).

Overall it is not good to see C-style memory management in C++ code, but I guess there are reasons for it 😃