diff options
author | Lei YU <mine260309@gmail.com> | 2019-01-25 16:43:50 +0800 |
---|---|---|
committer | Vernon Mauery <vernon.mauery@linux.intel.com> | 2019-02-12 18:02:40 +0000 |
commit | 4b0ddb68b4e76420358589213bc400155fa12e43 (patch) | |
tree | 1f439a640b8d6df9799213db644c4a272e6a7f9c /read_fru_data.cpp | |
parent | cf059392984d089b9044250e44f7ac039c5b032d (diff) | |
download | phosphor-host-ipmid-4b0ddb68b4e76420358589213bc400155fa12e43.tar.gz phosphor-host-ipmid-4b0ddb68b4e76420358589213bc400155fa12e43.zip |
Set init_priority attribute for global variables in shared lib
The code gets unspecified initializatio order for the global/static
variables in a shared library.
If unluck, a global/static variable may be initialized in *constructor*
function, and then initialized by the default contructor.
For exmaple, if `std::unique_ptr<xxx> var{nullptr};` is initialized in
constructor function, below init order may occur:
1. It is initialized in constructor;
2. Then it is initialized as nullptr;
And eventually when the code is to use the variable, we got nullptr.
We met such issues before on openbmc/openbmc#1581, and the technical
details could be found at [this SO question][1] and [this gcc email][2]
The solution is to specify the init_priority attribute, to make the
global/staic variables inititalize earlier than the contructors.
[1]: https://stackoverflow.com/questions/43941159/global-static-variables-initialization-issue-with-attribute-constructor-i
[2]: https://gcc.gnu.org/ml/gcc-patches/2017-03/msg00863.html
Change-Id: I901a6a5cddec12aec9512fe58b16735fa2ad90d7
Signed-off-by: Lei YU <mine260309@gmail.com>
Diffstat (limited to 'read_fru_data.cpp')
-rw-r--r-- | read_fru_data.cpp | 3 |
1 files changed, 2 insertions, 1 deletions
diff --git a/read_fru_data.cpp b/read_fru_data.cpp index 2bb111f..94b2478 100644 --- a/read_fru_data.cpp +++ b/read_fru_data.cpp @@ -23,7 +23,8 @@ namespace variant_ns = sdbusplus::message::variant_ns; using namespace phosphor::logging; using InternalFailure = sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; -std::unique_ptr<sdbusplus::bus::match_t> matchPtr(nullptr); +std::unique_ptr<sdbusplus::bus::match_t> matchPtr + __attribute__((init_priority(101))); static constexpr auto INV_INTF = "xyz.openbmc_project.Inventory.Manager"; static constexpr auto OBJ_PATH = "/xyz/openbmc_project/inventory"; |