From 1947220159a2b925f98d06825cf4b376d1cbac13 Mon Sep 17 00:00:00 2001 From: etherealjoy <33183834+etherealjoy@users.noreply.github.com> Date: Thu, 8 Mar 2018 17:19:45 +0100 Subject: [PATCH] Qt5cpp plug memleaks part2 (#7792) * Small fixes to prevent crash when empty json body is provided. * Fix some more memory Leaks in the model-body - Members not deleted in cleanup() method, for maps/arrays of primitive types. - Avoid undefined behavior when updating class members with data from missing json fields --- .../src/main/resources/qt5cpp/helpers-body.mustache | 2 +- .../src/main/resources/qt5cpp/model-body.mustache | 9 ++++++++- samples/client/petstore/qt5cpp/client/SWGHelpers.cpp | 2 +- samples/client/petstore/qt5cpp/client/SWGHelpers.h | 1 + samples/client/petstore/qt5cpp/client/SWGModelFactory.h | 1 + samples/client/petstore/qt5cpp/client/SWGObject.h | 2 +- 6 files changed, 13 insertions(+), 4 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/qt5cpp/helpers-body.mustache b/modules/swagger-codegen/src/main/resources/qt5cpp/helpers-body.mustache index 0b35496ff64..ca5f9116742 100644 --- a/modules/swagger-codegen/src/main/resources/qt5cpp/helpers-body.mustache +++ b/modules/swagger-codegen/src/main/resources/qt5cpp/helpers-body.mustache @@ -14,7 +14,7 @@ namespace {{this}} { void setValue(void* value, QJsonValue obj, QString type, QString complexType) { - if(value == nullptr) { + if((value == nullptr) || (obj.isUndefined())) { // can't set value with a null pointer return; } diff --git a/modules/swagger-codegen/src/main/resources/qt5cpp/model-body.mustache b/modules/swagger-codegen/src/main/resources/qt5cpp/model-body.mustache index 7728ac58369..d903823911a 100644 --- a/modules/swagger-codegen/src/main/resources/qt5cpp/model-body.mustache +++ b/modules/swagger-codegen/src/main/resources/qt5cpp/model-body.mustache @@ -47,7 +47,14 @@ void delete o; }{{/isContainer}} delete {{name}}; - }{{/complexType}} + }{{/complexType}}{{^complexType}}{{#isContainer}} + if({{name}} != nullptr) { {{#items.isContainer}} + auto arr = {{name}}; + for(auto o: *arr) { + delete o; + }{{/items.isContainer}} + delete {{name}}; + }{{/isContainer}}{{/complexType}} {{/vars}} } diff --git a/samples/client/petstore/qt5cpp/client/SWGHelpers.cpp b/samples/client/petstore/qt5cpp/client/SWGHelpers.cpp index 3da7251ff14..3c45ce56068 100644 --- a/samples/client/petstore/qt5cpp/client/SWGHelpers.cpp +++ b/samples/client/petstore/qt5cpp/client/SWGHelpers.cpp @@ -23,7 +23,7 @@ namespace Swagger { void setValue(void* value, QJsonValue obj, QString type, QString complexType) { - if(value == nullptr) { + if((value == nullptr) || (obj.isUndefined())) { // can't set value with a null pointer return; } diff --git a/samples/client/petstore/qt5cpp/client/SWGHelpers.h b/samples/client/petstore/qt5cpp/client/SWGHelpers.h index bbb43e43d8a..e9f8b345569 100644 --- a/samples/client/petstore/qt5cpp/client/SWGHelpers.h +++ b/samples/client/petstore/qt5cpp/client/SWGHelpers.h @@ -13,6 +13,7 @@ #ifndef SWG_HELPERS_H #define SWG_HELPERS_H +#include #include #include #include diff --git a/samples/client/petstore/qt5cpp/client/SWGModelFactory.h b/samples/client/petstore/qt5cpp/client/SWGModelFactory.h index dcea2ef855b..3e2fef6458e 100644 --- a/samples/client/petstore/qt5cpp/client/SWGModelFactory.h +++ b/samples/client/petstore/qt5cpp/client/SWGModelFactory.h @@ -13,6 +13,7 @@ #ifndef ModelFactory_H_ #define ModelFactory_H_ +#include "SWGObject.h" #include "SWGApiResponse.h" #include "SWGCategory.h" diff --git a/samples/client/petstore/qt5cpp/client/SWGObject.h b/samples/client/petstore/qt5cpp/client/SWGObject.h index 7f953ae59ec..a7a2af3809d 100644 --- a/samples/client/petstore/qt5cpp/client/SWGObject.h +++ b/samples/client/petstore/qt5cpp/client/SWGObject.h @@ -13,7 +13,7 @@ #ifndef _SWG_OBJECT_H_ #define _SWG_OBJECT_H_ -#include +#include namespace Swagger {