dsm-api does not know string type. It knows only array of bytes. Passing std::string::c_string() where dsm-api exptect array of 21 bytes leads to dsm-api reading all 21 bytes, it does not end the read at zero terminating character.
This patch fixes the problem, but is hackish. The functionality does not belong to truncateUTF8String.
=================================================================
==24925==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7ff496778770 at pc 0x0000009fabbb bp 0x7ff4967784f0 sp 0x7ff496777cb0
READ of size 21 at 0x7ff496778770 thread T68
#0 0x9fabba in memcpy /home/nikola/final/llvm.src/projects/compiler-rt/lib/asan/asan_interceptors.cc:438:3
#1 0x7ff4d21670b2 in ClusterProperties_set_name /home/brano/dss/dsm-api/src/generated/dsm-api-v2.c:209
#2 0x12d8358 in dss::DSStructureModifyingBusInterface::clusterSetName(unsigned char, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) /home/brano/dss/dss-mainline/build/../src/ds485/dsstructuremodifyingbusinterface.cpp:458:11
#3 0x11b6f0e in dss::synchronizeGroups(dss::Apartment*, dss::StructureModifyingBusInterface*) /home/brano/dss/dss-mainline/build/../src/util.cpp:64:11
#4 0xe146d5 in dss::MeterMaintenance::setupInitializedState() /home/brano/dss/dss-mainline/build/../src/model/modelmaintenance.cpp:267:5
#5 0xe0f31d in dss::MeterMaintenance::readOutPendingMeter() /home/brano/dss/dss-mainline/build/../src/model/modelmaintenance.cpp:259:9
#6 0xe0dfd3 in dss::MeterMaintenance::execute() /home/brano/dss/dss-mainline/build/../src/model/modelmaintenance.cpp:196:7
#7 0x14c1aed in dss::Thread::ThreadStarterHelperFunc(void*) /home/brano/dss/dss-mainline/build/../unix/thread.cpp:48:3
#8 0x7ff4ceeff5c9 in start_thread (/lib64/libpthread.so.0+0x75c9)
#9 0x7ff4ce188f6c in __clone (/lib64/libc.so.6+0x102f6c)
Address 0x7ff496778770 is located in stack of thread T68 at offset 176 in frame
#0 0x12d81bf in dss::DSStructureModifyingBusInterface::clusterSetName(unsigned char, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) /home/brano/dss/dss-mainline/build/../src/ds485/dsstructuremodifyingbusinterface.cpp:451
This frame has 4 object(s):
[32, 48) 'lock'
[64, 96) ''
[128, 129) ''
[144, 176) 'nameStr' <== Memory access at offset 176 overflows this variable
HINT: this may be a false positive if your program uses some custom stack unwind mechanism or swapcontext
(longjmp and C++ exceptions *are* supported)
Thread T68 created by T0 here:
#0 0x9f9039 in __interceptor_pthread_create /home/nikola/final/llvm.src/projects/compiler-rt/lib/asan/asan_interceptors.cc:238:3
#1 0x14c27ad in dss::Thread::run() /home/brano/dss/dss-mainline/build/../unix/thread.cpp:79:13
#2 0xe1bcb1 in dss::ModelMaintenance::doStart() /home/brano/dss/dss-mainline/build/../src/model/modelmaintenance.cpp:583:5
#3 0x1198390 in dss::Subsystem::start() /home/brano/dss/dss-mainline/build/../src/subsystem.cpp:78:7
#4 0xa9357b in dss::DSS::run() /home/brano/dss/dss-mainline/build/../src/dss.cpp:712:7
#5 0xa3f383 in main /home/brano/dss/dss-mainline/build/../main.cpp:242:5
#6 0x7ff4ce0a6730 in __libc_start_main (/lib64/libc.so.6+0x20730)