From 9102f669483b16a1823cf898747f5a809399289d Mon Sep 17 00:00:00 2001 From: Mira Grudzinska Date: Mon, 3 Apr 2023 22:45:26 +0200 Subject: [PATCH] examples: preventing undef behavior Calling get() on a unique pointer after it has been moved is the undefined behavior. --- src/examples/ImageScaleDown.cpp | 5 +++-- src/examples/ImageScaleUp.cpp | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/examples/ImageScaleDown.cpp b/src/examples/ImageScaleDown.cpp index 81928d03..6ef31c64 100644 --- a/src/examples/ImageScaleDown.cpp +++ b/src/examples/ImageScaleDown.cpp @@ -40,8 +40,9 @@ void tvgDrawCmds(tvg::Canvas* canvas) cout << "The PNG file is not loaded correctly. Did you enable PNG Loader?" << endl; return; } - if (canvas->push(move(picture)) == tvg::Result::Success) { - pPicture = picture.get(); + pPicture = picture.get(); + if (canvas->push(move(picture)) != tvg::Result::Success) { + pPicture = nullptr; } } diff --git a/src/examples/ImageScaleUp.cpp b/src/examples/ImageScaleUp.cpp index f45b96ca..45c6d8b5 100644 --- a/src/examples/ImageScaleUp.cpp +++ b/src/examples/ImageScaleUp.cpp @@ -40,8 +40,9 @@ void tvgDrawCmds(tvg::Canvas* canvas) cout << "The PNG file is not loaded correctly. Did you enable PNG Loader?" << endl; return; } - if (canvas->push(move(picture)) == tvg::Result::Success) { - pPicture = picture.get(); + pPicture = picture.get(); + if (canvas->push(move(picture)) != tvg::Result::Success) { + pPicture = nullptr; } }