Skip to content

Commit

Permalink
openpower-pels: fru-identity: fix crash in setPartNumber
Browse files Browse the repository at this point in the history
On newer libstdc++ implementations, the following backtrace is
observed:

```
 #2  0x00007ffff7a578b7 in abort () from /usr/lib64/libc.so.6
 #3  0x00007ffff7cda2af in std::__glibcxx_assert_fail(char const*, int, char const*, char const*) () from /usr/lib/gcc/x86_64-pc-linux-gnu/14/libstdc++.so.6
 #4  0x000055555556ac6f in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::front (this=0x7fffffffd790) at /usr/lib/gcc/x86_64-pc-linux-gnu/13/include/g++-v13/bits/basic_string.h:1315
 #5  std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::front (this=0x7fffffffd790) at /usr/lib/gcc/x86_64-pc-linux-gnu/13/include/g++-v13/bits/basic_string.h:1315
 #6  openpower::pels::src::FRUIdentity::setPartNumber (this=this@entry=0x7fffffffd8a0, partNumber="") at ../extensions/openpower-pels/fru_identity.cpp:216
 #7  0x000055555556ae12 in openpower::pels::src::FRUIdentity::FRUIdentity (this=this@entry=0x7fffffffd8a0, partNumber="", ccin="", serialNumber="") at ../extensions/openpower-pels/fru_identity.cpp:102
 #8  0x0000555555562aaf in testHWCallout (pn="", ccin="", sn="", expectedPN="", expectedCCIN="", expectedSN="") at ../test/openpower-pels/fru_identity_test.cpp:97
 #9  0x00005555555645b0 in FRUIdentityTest_CreateHardwareCalloutTest_Test::TestBody (this=<optimized out>) at /usr/lib/gcc/x86_64-pc-linux-gnu/13/include/g++-v13/bits/basic_string.tcc:242
```

Fix this by avoiding accessing `front()` when the part number string
is empty.  While there, do a minor performance optimization to avoid
unnecessary string copies, by using `erase` instead of `substr`.

Signed-off-by: Patrick Williams <[email protected]>
Change-Id: I71cc195596def6ad0cd982e7294e2467beee987c
  • Loading branch information
williamspatrick committed Oct 1, 2024
1 parent 70e8a11 commit 66f3675
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions extensions/openpower-pels/fru_identity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,9 @@ void FRUIdentity::setPartNumber(const std::string& partNumber)
auto pn = partNumber;

// Strip leading whitespace on this one.
while (' ' == pn.front())
while (!pn.empty() && (' ' == pn.front()))
{
pn = pn.substr(1);
pn.erase(0, 1);
}

fillArray(pn, _pnOrProcedureID);
Expand Down

0 comments on commit 66f3675

Please sign in to comment.