Author Topic: warning: 'parent' may be used uninitialized when building ToolsPlus\shellpropert  (Read 1310 times)

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5362
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
When building with GCC 11.1 compiler, I got such warning:

Code
[ 71.4%] g++.exe -Wall -pipe -mthreads -fmessage-length=0 -fexceptions -DWXUSINGDLL -DHAVE_W32API_H -D__WXMSW__ -D_WIN64 -DcbDEBUG -DNOPCH -DwxUSE_UNICODE -DBUILDING_PLUGIN -std=gnu++11 -g -I..\..\..\include -I..\..\..\include\tinyxml -I..\..\..\sdk\wxscintilla\include -Id:\code\wxWidgets-3.1.5\include -Id:\code\wxWidgets-3.1.5\lib\gcc_dll\mswu -c D:\code\cbsource\codeblocks_sf\src\plugins\contrib\ToolsPlus\shellproperties.cpp -o ..\..\..\.objs31_64\plugins\contrib\ToolsPlus\shellproperties.o
D:\code\cbsource\codeblocks_sf\src\plugins\contrib\ToolsPlus\se_globals.cpp: In function 'wxString GetParentDir(const wxString&)':
D:\code\cbsource\codeblocks_sf\src\plugins\contrib\ToolsPlus\se_globals.cpp:5:21: warning: 'parent' may be used uninitialized [-Wmaybe-uninitialized]
    5 |     wxString parent=wxFileName(parent).GetPath(0);
      |                     ^~~~~~~~~~~~~~~~~~
In file included from D:\code\cbsource\codeblocks_sf\src\plugins\contrib\ToolsPlus\se_globals.h:12,
                 from D:\code\cbsource\codeblocks_sf\src\plugins\contrib\ToolsPlus\se_globals.cpp:1:
d:\code\wxWidgets-3.1.5\include/wx/filename.h:138:5: note: by argument 2 of type 'const wxString&' to 'wxFileName::wxFileName(const wxString&, wxPathFormat)' declared here
  138 |     wxFileName( const wxString& fullpath, wxPathFormat format = wxPATH_NATIVE )
      |     ^~~~~~~~~~
D:\code\cbsource\codeblocks_sf\src\plugins\contrib\ToolsPlus\se_globals.cpp:5:14: note: 'parent' declared here
    5 |     wxString parent=wxFileName(parent).GetPath(0);
      |              ^~~~~~

I see the code is:
Code
wxString GetParentDir(const wxString &path)
{
    wxString parent=wxFileName(parent).GetPath(0);
    if(path==parent||parent.IsEmpty())
        return wxEmptyString;
    else
        return parent;
}

So, this is a bug?
I think

Code
wxString parent=wxFileName(parent).GetPath(0);

should be

Code
wxString parent=wxFileName(path).GetPath(0);
?

If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5362
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Code
D:\code\cbsource\codeblocks_sf\src\plugins\contrib\FileManager\se_globals.cpp: In function 'wxString GetParentDir(const wxString&)':
D:\code\cbsource\codeblocks_sf\src\plugins\contrib\FileManager\se_globals.cpp:6:21: warning: 'parent' may be used uninitialized [-Wmaybe-uninitialized]
    6 |     wxString parent=wxFileName(parent).GetPath(0);
      |                     ^~~~~~~~~~~~~~~~~~
In file included from D:\code\cbsource\codeblocks_sf\src\plugins\contrib\FileManager\se_globals.h:12,
                 from D:\code\cbsource\codeblocks_sf\src\plugins\contrib\FileManager\se_globals.cpp:2:
d:\code\wxWidgets-3.1.5\include/wx/filename.h:138:5: note: by argument 2 of type 'const wxString&' to 'wxFileName::wxFileName(const wxString&, wxPathFormat)' declared here
  138 |     wxFileName( const wxString& fullpath, wxPathFormat format = wxPATH_NATIVE )
      |     ^~~~~~~~~~
D:\code\cbsource\codeblocks_sf\src\plugins\contrib\FileManager\se_globals.cpp:6:14: note: 'parent' declared here
    6 |     wxString parent=wxFileName(parent).GetPath(0);
      |              ^~~~~~


I see there is a similar warning in FileManager\se_globals.cpp.

There are two "se_globals.cpp" in our code base. :)
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13408
    • Travis build status
Just fix it.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5362
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.