Skip to content

[Q] Crash from dereferencing uninitialized V_POINTER Variant (?) #34

@Ngiong

Description

@Ngiong

Hi.
While trying this library, I found that the pointer Variant can only be initialized using internalSetPtr() method call.
However, it is still possible to create an uninitialized pointer Variant through Variant(Type) constructor which could lead the program to crash when it is dereferenced, as shown in the following code snippet.

#include "var.h"

int main() {
  jvar::Variant variant3(jvar::Variant::Type::V_POINTER); // illegal
  variant3.toJsonString(); // crash
}

Do you think it's worth preventing such crashes by disallowing uninitialized pointer-type Variant creation from the Variant(Type) constructor? I think the library would be safer if the library forces the users to construct such pointer Variant only by calling internalSetPtr(...) explicitly, for example:

#include "var.h"

int main() {
  jvar::Variant variant3; // empty variant first
  variant3.internalSetPtr(...); // initializes pointer variant
  variant3.toJsonString();
}

CMIIW.
Thank you.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions