Skip to content

The fleet provisioning sample code in samples/fleet_provisioning/provision-basic/main.cpp contains dangling reference warnings that cause compilation failures when using newer GCC versions with -Werror #839

@thomas-roos

Description

@thomas-roos

Describe the bug

Error Details

error: possibly dangling reference to a temporary [-Werror=dangling-reference]
const auto &createKeysResult = createKeysResultPromise.get_future().get().value();

Affected Lines

  • Line 132: const auto &createKeysResult = createKeysResultPromise.get_future().get().value();
  • Line 172: const auto &registerThingResult = registerThingResultPromise.get_future().get().value();

Root Cause

The code binds references to temporary objects that are destroyed at the end of the expression, creating dangling references.

Proposed Fix

Change references to values:
cpp
// Before
const auto &createKeysResult = createKeysResultPromise.get_future().get().value();
const auto &registerThingResult = registerThingResultPromise.get_future().get().value();

// After
const auto createKeysResult = createKeysResultPromise.get_future().get().value();
const auto registerThingResult = registerThingResultPromise.get_future().get().value();

Environment

  • Compiler: GCC with -Wdangling-reference warning enabled
  • Build system: Yocto with -Werror flag

Current Behavior

Log data follows:
| DEBUG: Executing shell function do_compile
| NOTE: VERBOSE=1 cmake --build /home/ubuntu/data/yocto_walnascar/build/tmp/work/core2-64-poky-linux/aws-iot-device-sdk-cpp-v2-samples-fleet-provisoning/1.0/build --target all --
| Change Dir: '/home/ubuntu/data/yocto_walnascar/build/tmp/work/core2-64-poky-linux/aws-iot-device-sdk-cpp-v2-samples-fleet-provisoning/1.0/build'
| 
| Run Build Command(s): ninja -v -j 64 all
| [1/2] /home/ubuntu/data/yocto_walnascar/build/tmp/work/core2-64-poky-linux/aws-iot-device-sdk-cpp-v2-samples-fleet-provisoning/1.0/recipe-sysroot-native/usr/bin/x86_64-poky-linux/x86_64-poky-linux-g++ --sysroot=/home/ubuntu/data/yocto_walnascar/build/tmp/work/core2-64-poky-linux/aws-iot-device-sdk-cpp-v2-samples-fleet-provisoning/1.0/recipe-sysroot -DAWS_ENABLE_EPOLL -DAWS_IOTIDENTITY_USE_IMPORT_EXPORT  -m64 -march=core2 -mtune=core2 -msse3 -mfpmath=sse -fstack-protector-strong  -O2 -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=format-security  --sysroot=/home/ubuntu/data/yocto_walnascar/build/tmp/work/core2-64-poky-linux/aws-iot-device-sdk-cpp-v2-samples-fleet-provisoning/1.0/recipe-sysroot  -O2 -g -fcanon-prefix-map  -fmacro-prefix-map=/home/ubuntu/data/yocto_walnascar/build/tmp/work/core2-64-poky-linux/aws-iot-device-sdk-cpp-v2-samples-fleet-provisoning/1.0/sources-unpack/git/samples/fleet_provisioning/provision-basic=/usr/src/debug/aws-iot-device-sdk-cpp-v2-samples-fleet-provisoning/1.0  -fdebug-prefix-map=/home/ubuntu/data/yocto_walnascar/build/tmp/work/core2-64-poky-linux/aws-iot-device-sdk-cpp-v2-samples-fleet-provisoning/1.0/sources-unpack/git/samples/fleet_provisioning/provision-basic=/usr/src/debug/aws-iot-device-sdk-cpp-v2-samples-fleet-provisoning/1.0  -fmacro-prefix-map=/home/ubuntu/data/yocto_walnascar/build/tmp/work/core2-64-poky-linux/aws-iot-device-sdk-cpp-v2-samples-fleet-provisoning/1.0/build=/usr/src/debug/aws-iot-device-sdk-cpp-v2-samples-fleet-provisoning/1.0  -fdebug-prefix-map=/home/ubuntu/data/yocto_walnascar/build/tmp/work/core2-64-poky-linux/aws-iot-device-sdk-cpp-v2-samples-fleet-provisoning/1.0/build=/usr/src/debug/aws-iot-device-sdk-cpp-v2-samples-fleet-provisoning/1.0  -fdebug-prefix-map=/home/ubuntu/data/yocto_walnascar/build/tmp/work/core2-64-poky-linux/aws-iot-device-sdk-cpp-v2-samples-fleet-provisoning/1.0/recipe-sysroot=  -fmacro-prefix-map=/home/ubuntu/data/yocto_walnascar/build/tmp/work/core2-64-poky-linux/aws-iot-device-sdk-cpp-v2-samples-fleet-provisoning/1.0/recipe-sysroot=  -fdebug-prefix-map=/home/ubuntu/data/yocto_walnascar/build/tmp/work/core2-64-poky-linux/aws-iot-device-sdk-cpp-v2-samples-fleet-provisoning/1.0/recipe-sysroot-native=  -fmacro-prefix-map=/home/ubuntu/data/yocto_walnascar/build/tmp/work/core2-64-poky-linux/aws-iot-device-sdk-cpp-v2-samples-fleet-provisoning/1.0/recipe-sysroot-native=  -pipe -fvisibility-inlines-hidden -DNDEBUG -std=gnu++14 -Wall -Wno-long-long -pedantic -Werror -MD -MT CMakeFiles/fleet-provisioning-basic.dir/main.cpp.o -MF CMakeFiles/fleet-provisioning-basic.dir/main.cpp.o.d -o CMakeFiles/fleet-provisioning-basic.dir/main.cpp.o -c /home/ubuntu/data/yocto_walnascar/build/tmp/work/core2-64-poky-linux/aws-iot-device-sdk-cpp-v2-samples-fleet-provisoning/1.0/sources-unpack/git/samples/fleet_provisioning/provision-basic/main.cpp
| FAILED: CMakeFiles/fleet-provisioning-basic.dir/main.cpp.o
| /home/ubuntu/data/yocto_walnascar/build/tmp/work/core2-64-poky-linux/aws-iot-device-sdk-cpp-v2-samples-fleet-provisoning/1.0/recipe-sysroot-native/usr/bin/x86_64-poky-linux/x86_64-poky-linux-g++ --sysroot=/home/ubuntu/data/yocto_walnascar/build/tmp/work/core2-64-poky-linux/aws-iot-device-sdk-cpp-v2-samples-fleet-provisoning/1.0/recipe-sysroot -DAWS_ENABLE_EPOLL -DAWS_IOTIDENTITY_USE_IMPORT_EXPORT  -m64 -march=core2 -mtune=core2 -msse3 -mfpmath=sse -fstack-protector-strong  -O2 -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=format-security  --sysroot=/home/ubuntu/data/yocto_walnascar/build/tmp/work/core2-64-poky-linux/aws-iot-device-sdk-cpp-v2-samples-fleet-provisoning/1.0/recipe-sysroot  -O2 -g -fcanon-prefix-map  -fmacro-prefix-map=/home/ubuntu/data/yocto_walnascar/build/tmp/work/core2-64-poky-linux/aws-iot-device-sdk-cpp-v2-samples-fleet-provisoning/1.0/sources-unpack/git/samples/fleet_provisioning/provision-basic=/usr/src/debug/aws-iot-device-sdk-cpp-v2-samples-fleet-provisoning/1.0  -fdebug-prefix-map=/home/ubuntu/data/yocto_walnascar/build/tmp/work/core2-64-poky-linux/aws-iot-device-sdk-cpp-v2-samples-fleet-provisoning/1.0/sources-unpack/git/samples/fleet_provisioning/provision-basic=/usr/src/debug/aws-iot-device-sdk-cpp-v2-samples-fleet-provisoning/1.0  -fmacro-prefix-map=/home/ubuntu/data/yocto_walnascar/build/tmp/work/core2-64-poky-linux/aws-iot-device-sdk-cpp-v2-samples-fleet-provisoning/1.0/build=/usr/src/debug/aws-iot-device-sdk-cpp-v2-samples-fleet-provisoning/1.0  -fdebug-prefix-map=/home/ubuntu/data/yocto_walnascar/build/tmp/work/core2-64-poky-linux/aws-iot-device-sdk-cpp-v2-samples-fleet-provisoning/1.0/build=/usr/src/debug/aws-iot-device-sdk-cpp-v2-samples-fleet-provisoning/1.0  -fdebug-prefix-map=/home/ubuntu/data/yocto_walnascar/build/tmp/work/core2-64-poky-linux/aws-iot-device-sdk-cpp-v2-samples-fleet-provisoning/1.0/recipe-sysroot=  -fmacro-prefix-map=/home/ubuntu/data/yocto_walnascar/build/tmp/work/core2-64-poky-linux/aws-iot-device-sdk-cpp-v2-samples-fleet-provisoning/1.0/recipe-sysroot=  -fdebug-prefix-map=/home/ubuntu/data/yocto_walnascar/build/tmp/work/core2-64-poky-linux/aws-iot-device-sdk-cpp-v2-samples-fleet-provisoning/1.0/recipe-sysroot-native=  -fmacro-prefix-map=/home/ubuntu/data/yocto_walnascar/build/tmp/work/core2-64-poky-linux/aws-iot-device-sdk-cpp-v2-samples-fleet-provisoning/1.0/recipe-sysroot-native=  -pipe -fvisibility-inlines-hidden -DNDEBUG -std=gnu++14 -Wall -Wno-long-long -pedantic -Werror -MD -MT CMakeFiles/fleet-provisioning-basic.dir/main.cpp.o -MF CMakeFiles/fleet-provisioning-basic.dir/main.cpp.o.d -o CMakeFiles/fleet-provisioning-basic.dir/main.cpp.o -c /home/ubuntu/data/yocto_walnascar/build/tmp/work/core2-64-poky-linux/aws-iot-device-sdk-cpp-v2-samples-fleet-provisoning/1.0/sources-unpack/git/samples/fleet_provisioning/provision-basic/main.cpp
| /home/ubuntu/data/yocto_walnascar/build/tmp/work/core2-64-poky-linux/aws-iot-device-sdk-cpp-v2-samples-fleet-provisoning/1.0/sources-unpack/git/samples/fleet_provisioning/provision-basic/main.cpp: In function 'int main(int, char**)':
| /home/ubuntu/data/yocto_walnascar/build/tmp/work/core2-64-poky-linux/aws-iot-device-sdk-cpp-v2-samples-fleet-provisoning/1.0/sources-unpack/git/samples/fleet_provisioning/provision-basic/main.cpp:132:17: error: possibly dangling reference to a temporary [-Werror=dangling-reference]
|   132 |     const auto &createKeysResult = createKeysResultPromise.get_future().get().value();
|       |                 ^~~~~~~~~~~~~~~~
| /home/ubuntu/data/yocto_walnascar/build/tmp/work/core2-64-poky-linux/aws-iot-device-sdk-cpp-v2-samples-fleet-provisoning/1.0/sources-unpack/git/samples/fleet_provisioning/provision-basic/main.cpp:132:84: note: the temporary was destroyed at the end of the full expression 'std::future<_Res>::get() [with _Res = Aws::Crt::Optional<Aws::Iot::RequestResponse::Result<Aws::Iotidentity::CreateKeysAndCertificateResponse, Aws::Iotidentity::ServiceErrorV2<Aws::Iotidentity::V2ErrorResponse> > >]().Aws::Crt::Optional<Aws::Iot::RequestResponse::Result<Aws::Iotidentity::CreateKeysAndCertificateResponse, Aws::Iotidentity::ServiceErrorV2<Aws::Iotidentity::V2ErrorResponse> > >::value()'
|   132 |     const auto &createKeysResult = createKeysResultPromise.get_future().get().value();
|       |                                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
| /home/ubuntu/data/yocto_walnascar/build/tmp/work/core2-64-poky-linux/aws-iot-device-sdk-cpp-v2-samples-fleet-provisoning/1.0/sources-unpack/git/samples/fleet_provisioning/provision-basic/main.cpp:172:17: error: possibly dangling reference to a temporary [-Werror=dangling-reference]
|   172 |     const auto &registerThingResult = registerThingResultPromise.get_future().get().value();
|       |                 ^~~~~~~~~~~~~~~~~~~
| /home/ubuntu/data/yocto_walnascar/build/tmp/work/core2-64-poky-linux/aws-iot-device-sdk-cpp-v2-samples-fleet-provisoning/1.0/sources-unpack/git/samples/fleet_provisioning/provision-basic/main.cpp:172:90: note: the temporary was destroyed at the end of the full expression 'std::future<_Res>::get() [with _Res = Aws::Crt::Optional<Aws::Iot::RequestResponse::Result<Aws::Iotidentity::RegisterThingResponse, Aws::Iotidentity::ServiceErrorV2<Aws::Iotidentity::V2ErrorResponse> > >]().Aws::Crt::Optional<Aws::Iot::RequestResponse::Result<Aws::Iotidentity::RegisterThingResponse, Aws::Iotidentity::ServiceErrorV2<Aws::Iotidentity::V2ErrorResponse> > >::value()'
|   172 |     const auto &registerThingResult = registerThingResultPromise.get_future().get().value();
|       |                                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
| cc1plus: all warnings being treated as errors
| ninja: build stopped: subcommand failed.
| 
| WARNING: exit code 1 from a shell command.

Reproduction Steps

build with CXXFLAGS:append = " -Wdangling-reference"

Possible Solution

No response

Additional Information/Context

No response

SDK version used

1.40.1

Environment details (OS name and version, etc.)

Yocto / meta-aws

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugThis issue is a bug.needs-triageThis issue or PR still needs to be triaged.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions