To get a trial key
fill out the form below
Team License (standard version)
Enterprise License (extended version)
* By clicking this button you agree to our Privacy Policy statement

** This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
Request our prices
New License
License Renewal
--Select currency--
USD
EUR
GBP
RUB
* By clicking this button you agree to our Privacy Policy statement

** This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
Message submitted.

Your message has been sent. We will email you at


If you haven't received our response, please do the following:
check your Spam/Junk folder and click the "Not Spam" button for our message.
This way, you won't miss messages from our team in the future.

>
>
>
A Check of the Open-Source Project WinS…

A Check of the Open-Source Project WinSCP Developed in Embarcadero C++ Builder

April 24, 2014
Author:

We regularly check open-source C/C++ projects, but what we check are mostly projects developed in the Visual Studio IDE. For some reason, we haven't paid much attention to the Embarcadero C++ Builder IDE. In order to improve this situation, we are going to discuss the WinSCP project I have checked recently.

C++ Builder support in PVS-Studio had been dropped after version 5.20. If you have any questions, feel free to contact our support.

0252_WinSCP/image1.png

WinSCP

WinSCP is a free and open-source SFTP, SCP and FTP client for Microsoft Windows. Its main function is secure file transfer between a local and a remote computer. Beyond this, WinSCP offers basic file manager and file synchronization functionality. Since July 16, 2003, it is licensed under the GNU GPL and hosted on SourceForge.net.

The official website: http://winscp.net

You need Embarcadero C++ Builder XE2 to build the project.

Analysis

Analysis was done with the PVS-Studio static analyzer. The tool currently supports the following IDEs:

  • Visual Studio 2013 C, C++, C++11, C++/CX (WinRT)
  • Visual Studio 2012 C, C++, C++11, C++/CX (WinRT)
  • Visual Studio 2010 C, C++, C++0x
  • Visual Studio 2008 C, C++
  • Visual Studio 2005 C, C++
  • Embarcadero RAD Studio XE5 C, C++, C++11, 64-bit compiler included
  • Embarcadero RAD Studio XE4 C, C++, C++11, 64-bit compiler included
  • Embarcadero RAD Studio XE3 Update 1 C, C++, C++11, 64-bit compiler included
  • Embarcadero RAD Studio XE2 C, C++, C++0x
  • Embarcadero RAD Studio XE C, C++
  • Embarcadero RAD Studio 2010 C, C++
  • Embarcadero RAD Studio 2009 C, C++
  • MinGW C, C++, C++11

Besides, you can also work in PVS-Studio Standalone. It allows checking *.i files prepared in advance and monitoring the project build process in order to collect all the necessary information for a check. To find out more about it, see the article "PVS-Studio Now Supports Any Build System under Windows and Any Compiler".

Analysis results

There are not many errors in the project - but still enough to write an article to attract Embarcadero RAD Studio users' attention.

Memset() function's arguments mixed up

TForm * __fastcall TMessageForm::Create(....)
{
  ....
  LOGFONT AFont;
  ....   
  memset(&AFont, sizeof(AFont), 0);
  ....
}

PVS-Studio's diagnostic message: V575 The 'memset' function processes '0' elements. Inspect the third argument. messagedlg.cpp 786

The memset() function receives an array size as the third argument. It's an ordinary but very unpleasant typo resulting in the structure remaining uninitialized.

There is a similar typo a bit farther in the code: messagedlg.cpp 796

Using a nonexistent object

void __fastcall TCustomScpExplorerForm::EditorAutoConfig()
{
  ....
  else
  {
    ....
    TEditorList EditorList;
    EditorList = *WinConfiguration->EditorList;
    EditorList.Insert(0, new TEditorPreferences(EditorData));
    WinConfiguration->EditorList = &EditorList;
  }
  ....
}

PVS-Studio's diagnostic message: V506 Pointer to local variable 'EditorList' is stored outside the scope of this variable. Such a pointer will become invalid. customscpexplorer.cpp 2633

The 'EditorList' object will be destroyed immediately after leaving the scope. However, the programmer saves a pointer to this object and uses it after that. It leads to undefined behavior.

Garbage in a dialog

bool __fastcall RecursiveDeleteFile(....)
{
  SHFILEOPSTRUCT Data;
  memset(&Data, 0, sizeof(Data));
  ....
  Data.pTo = L"";
  ....
}

PVS-Studio's diagnostic message: V540 Member 'pTo' should point to string terminated by two 0 characters. common.cpp 1659

Notice the following line in the pTo parameter's description in MSDN: "Note This string must be double-null terminated".

Because of the error, the file-handling dialog will contain garbage - or it will not. It all depends on how lucky you are. But the code is incorrect anyway.

A duplicated line

int CFileZillaApi::Init(....)
{
  ....
  m_pMainThread->m_hOwnerWnd=m_hOwnerWnd;
  m_pMainThread->m_hOwnerWnd=m_hOwnerWnd;
  ....
}

PVS-Studio's diagnostic message: V519 The 'm_pMainThread->m_hOwnerWnd' variable is assigned values twice successively. Perhaps this is a mistake. Check lines: 88, 89. filezillaapi.cpp 89

Perhaps there is no error here - just one extra line written by mistake.

Failed check

STDMETHODIMP CShellExtClassFactory::CreateInstance(....)
{
  ....
  CShellExt* ShellExt = new CShellExt();
  if (NULL == ShellExt)
  {
    return E_OUTOFMEMORY;
  }
  ....
}

PVS-Studio's diagnostic message: V668 There is no sense in testing the 'ShellExt' pointer against null, as the memory was allocated using the 'new' operator. The exception will be generated in the case of memory allocation error. dragext.cpp 554

The check "if (NULL == ShellExt)" is meaningless as the 'new' operator will throw the std::bad_alloc exception if it fails to allocate memory.

A dangerous way to use the fprintf() function

bool CAsyncSslSocketLayer::CreateSslCertificate(....)
{
  ....
  char buffer[1001];
  int len;
  while ((len = pBIO_read(bio, buffer, 1000)) > 0)
  {
    buffer[len] = 0;
    fprintf(file, buffer);
  }
  ....
}

V618 It's dangerous to call the 'fprintf' function in such a manner, as the line being passed could contain format specification. The example of the safe code: printf("%s", str); asyncsslsocketlayer.cpp 2247

If the buffer contains control specifiers while data are being written into the file, it will lead to an unpredictable result. The safe way of using the function is as follows:

fprintf(file, "%s", buffer);

This error can also be treated as a potential vulnerability.

Something wrong with the 'err' variable

static error_t
client_send_propfind_request(....)
{
  ....
  error_t err = 0;
  int code = 0;

  apr_hash_t * props = NULL;
  const char * target = path_uri_encode(remote_path, pool);
  char * url_path = apr_pstrdup(pool, target);

  WEBDAV_ERR(neon_get_props(&props, ras, url_path,
    NEON_DEPTH_ZERO, starting_props,
    false, pool));

  if (err && (err == WEBDAV_ERR_DAV_REQUEST_FAILED))
  ....
}

PVS-Studio's diagnostic message: V560 A part of conditional expression is always false: (err == 1003). webdavfilesystem.cpp 10990

Conclusion

Where are you, Embarcadero RAD Studio users? Hey! According to our statistics, they are very few. Well, come and try the PVS-Studio static analyzer!

Popular related articles
Technologies used in the PVS-Studio code analyzer for finding bugs and potential vulnerabilities

Date: 11.21.2018

Author: Andrey Karpov

A brief description of technologies used in the PVS-Studio tool, which let us effectively detect a large number of error patterns and potential vulnerabilities. The article describes the implementati…
PVS-Studio ROI

Date: 01.30.2019

Author: Andrey Karpov

Occasionally, we're asked a question, what monetary value the company will receive from using PVS-Studio. We decided to draw up a response in the form of an article and provide tables, which will sho…
Free PVS-Studio for those who develops open source projects

Date: 12.22.2018

Author: Andrey Karpov

On the New 2019 year's eve, a PVS-Studio team decided to make a nice gift for all contributors of open-source projects hosted on GitHub, GitLab or Bitbucket. They are given free usage of PVS-Studio s…
The way static analyzers fight against false positives, and why they do it

Date: 03.20.2017

Author: Andrey Karpov

In my previous article I wrote that I don't like the approach of evaluating the efficiency of static analyzers with the help of synthetic tests. In that article, I give the example of a code fragment…
How PVS-Studio Proved to Be More Attentive Than Three and a Half Programmers

Date: 10.22.2018

Author: Andrey Karpov

Just like other static analyzers, PVS-Studio often produces false positives. What you are about to read is a short story where I'll tell you how PVS-Studio proved, just one more time, to be more atte…
PVS-Studio for Java

Date: 01.17.2019

Author: Andrey Karpov

In the seventh version of the PVS-Studio static analyzer, we added support of the Java language. It's time for a brief story of how we've started making support of the Java language, how far we've co…
Characteristics of PVS-Studio Analyzer by the Example of EFL Core Libraries, 10-15% of False Positives

Date: 07.31.2017

Author: Andrey Karpov

After I wrote quite a big article about the analysis of the Tizen OS code, I received a large number of questions concerning the percentage of false positives and the density of errors (how many erro…
Appreciate Static Code Analysis!

Date: 10.16.2017

Author: Andrey Karpov

I am really astonished by the capabilities of static code analysis even though I am one of the developers of PVS-Studio analyzer myself. The tool surprised me the other day as it turned out to be sma…
The Evil within the Comparison Functions

Date: 05.19.2017

Author: Andrey Karpov

Perhaps, readers remember my article titled "Last line effect". It describes a pattern I've once noticed: in most cases programmers make an error in the last line of similar text blocks. Now I want t…
The Ultimate Question of Programming, Refactoring, and Everything

Date: 04.14.2016

Author: Andrey Karpov

Yes, you've guessed correctly - the answer is "42". In this article you will find 42 recommendations about coding in C++ that can help a programmer avoid a lot of errors, save time and effort. The au…

Comments (0)

Next comments

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
This website uses cookies and other technology to provide you a more personalized experience. By continuing the view of our web-pages you accept the terms of using these files. If you don't want your personal data to be processed, please, leave this site.
Learn More →
Accept