Skip to content

Fix output truncation warning in RazerController.cpp

James Buren requested to merge braewoods/OpenRGB:fix-stringop-truncation into master

I've been seeing this warning on Linux for awhile now:

                 from Controllers/RazerController/RazerController/RazerController.cpp:12:
In function ‘char* strncpy(char*, const char*, size_t)’,
    inlined from ‘std::string RazerController::razer_get_serial()’ at Controllers/RazerController/RazerController/RazerController.cpp:1011:12:
/usr/include/x86_64-linux-gnu/bits/string_fortified.h:95:34: warning: ‘char* __builtin_strncpy(char*, const char*, long unsigned int)’ output may be truncated copying 22 bytes from a string of length 79 [-Wstringop-truncation]
   95 |   return __builtin___strncpy_chk (__dest, __src, __len,
      |          ~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
   96 |                                   __glibc_objsize (__dest));
      |                                   ~~~~~~~~~~~~~~~~~~~~~~~~~~

Given the surrounding code, it seems as if strncpy is being used like memcpy since it writes a null terminator explicitly at a fixed position and then uses a loop to filter out non-ASCII or unprintable characters up to this null terminator. This means the original author does not expect a null terminator to exist in the first 22 bytes. So memcpy should be functionally equivalent to strncpy in this context. Using this as a replacement fixes the warning.

Edited by James Buren

Merge request reports

Loading