[GOLD] gcc-11 stringop-overflow warning

Martin Sebor msebor@gmail.com
Wed Dec 9 17:34:47 GMT 2020


On 12/7/20 5:05 AM, Alan Modra via Binutils wrote:
> I'm unsure why this is deserving of a warning.  Not writing the most
> efficient code surely can't be a real problem, but that is what
> https://gcc.gnu.org/bugzilla//show_bug.cgi?id=88059#c1 seems to say.

Yes, the warning could stand to be relaxed a bit here.  It's on my
(long) list of things to do.  Sorry it's causing unnecessary churn.

FWIW, I tried to see if the code could be rewritten without resorting
to memcpy.  It seems to me that making tempdir a std::string like some
of the other local variables would do it.  tempdir is assigned to
this->tempdir_ which is also a pointer, but because the Plugin_recorder
class whose member it is defines no destructor and its finish() member
doesn't delete the tempdir_ pointer, the allocated memory leaks.
So changing the type of Plugin_recorder::tempdir_ to std::string would,
besides avoiding the warning and making the code "cleaner," also plug
this leak.  That seems like a win-win.

Martin

> 
> plugin.cc:528:10: error: ‘char* strncpy(char*, const char*, size_t)’ specified bound depends on the length of the source argument [-Werror=stringop-overflow=]
>    528 |   strncpy(tempdir, dir_template, len);
>        |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
> plugin.cc:526:22: note: length computed here
>    526 |   size_t len = strlen(dir_template) + 1;
>        |                ~~~~~~^~~~~~~~~~~~~~
> 
> Applied as obvious, or at least, not wrong.
> 
> 	* plugin.cc (Plugin_recorder::init): Replace strncpy with memcpy.
> 
> diff --git a/gold/plugin.cc b/gold/plugin.cc
> index 729ddca9f3..fd37957e73 100644
> --- a/gold/plugin.cc
> +++ b/gold/plugin.cc
> @@ -525,7 +525,7 @@ Plugin_recorder::init()
>   
>     size_t len = strlen(dir_template) + 1;
>     char* tempdir = new char[len];
> -  strncpy(tempdir, dir_template, len);
> +  memcpy(tempdir, dir_template, len);
>   
>     // Create the log file.
>     std::string logname(tempdir);
> 



More information about the Binutils mailing list