Skip to content

C interface for Exporter cannot get format descriptions #693

Closed

Description

@yig

The C interface for Exporter has a bug. The function aiGetExportFormatDescription( index ) performs Exporter().GetExportFormatDescription( index ). However, this returns temporary memory. Attempting to access the returned aiExportFormatDesc results in a segfault. The solution is either to return a pointer to the global array of aiExportFormatDesc or to return a copy of the aiExportFormatDesc struct with a corresponding free function.

The following fails:

// brew install assimp
// cc cexport_bug.c -o cexport_bug -I/usr/local/include -L/usr/local/lib -lassimp

#include <stdio.h>
#include <assimp/cexport.h>

int main( int argc, char* argv[] ) {
    // Get the count
    const size_t count = aiGetExportFormatCount();
    for( size_t i = 0; i < count; ++i )
    {
        // Get the description.
        const struct aiExportFormatDesc* desc = aiGetExportFormatDescription( i );

        // desc points to free'd memory. The following crashes or prints garbage.
        printf( "%s: %s (id: %s)\n", desc->fileExtension, desc->description, desc->id );
    }

    return 0;
}

The following works:

// brew install assimp
// c++ cexport_works.c -o cexport_works -I/usr/local/include -L/usr/local/lib -lassimp

#include <stdio.h>
#include <assimp/Exporter.hpp>

int main( int argc, char* argv[] ) {
    // Allocate an Exporter.
    Assimp::Exporter exporter;

    // Get the count
    const size_t count = exporter.GetExportFormatCount();
    for( size_t i = 0; i < count; ++i )
    {
        // Get the description.
        const aiExportFormatDesc* desc = exporter.GetExportFormatDescription( i );

        // desc does not point to free'd memory. The following works.
        printf( "%s: %s (id: %s)\n", desc->fileExtension, desc->description, desc->id );
    }

    return 0;
}

Activity

  1. kimkulling commented on Nov 23, 2015

    @kimkulling
    Member

    Now the description will be copied.

  2. kimkulling commented on Nov 23, 2015

    @kimkulling
    Member

    Thanks for reporting this issue!

  3. yig commented on Nov 24, 2015

    @yig
    ContributorAuthor

    I looked at the fix, but it seems to have the same problem: https://github.com/assimp/assimp/blob/master/code/AssimpCExport.cpp#L66

    The line

    const aiExportFormatDesc* orig( Exporter().GetExportFormatDescription( index ) );
    

    is still storing a pointer to an already free'd object.

    The correct solution is to create an Exporter at the beginning of the function and use it. I will create a pull request.

  4. added a commit that references this issue on Dec 11, 2023
  5. added a commit that references this issue on Feb 5, 2024
  6. added
    Export
    Bug
    on Apr 17, 2025
  7. added a commit that references this issue on Jul 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugExport

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions