>
>
>
A Tribute to Opening Up Dolphin Smallta…

Sviatoslav Razmyslov
Articles: 90

A Tribute to Opening Up Dolphin Smalltalk 7's Source Code

A few days ago, the ObjectArts company made their source code and the Dolphin Smalltalk IDE open, making it available under the MIT license! Of course, I couldn't miss the chance to try our PVS-Studio code analyzer on the project. Right off, my congratulations go to the developers: they really managed to create high-quality code which has no critical bugs. However, some bugs and smelling code are always to be found in any project, and I hope this article will help make the code a bit better.

About the project

Dolphin Smalltalk is an IDE for the implementation of the Smalltalk language by Object Arts, targeted at the Microsoft Windows platform. Its key features are tight integration with the operating system's native widgets and subsystems, including COM and ActiveX, and user-friendly GUI.

For a long time, Dolphin Smalltalk was available as two separate products: a shareware limited Community Edition and the commercial Professional Edition. The latter provided all the functions, including advanced editors and compilation of applications in standalone mode, but its price was about four hundred dollars.

I analyzed Dolphin Smalltalk Virtual Machine source code with PVS-Studio 6.00, and here are the analysis results. Although DolphinVM is a tiny project, there are still a few suspicious spots in it.

Analysis results

Warning No. 1: V611 The memory was allocated using 'new T[]' operator but was released using the 'delete' operator. Consider inspecting this code. It's probably better to use 'delete [] msg;'. compiler.cpp 379

Compiler::StaticType Compiler::FindNameAsStatic(....)
{
  ....
  char* msg = new char[strlen(szPrompt)+name.size()+32];
  ::wsprintf(msg, szPrompt, name.c_str());
  char szCaption[256];
  ::LoadString(GetResLibHandle(), IDR_COMPILER, szCaption, ....);
  int answer = ::MessageBox(NULL, msg, szCaption, ....);
  delete msg;  // <=??
  ....
}

The analyzer detected an error that has to do with allocating and freeing memory using inconsistent techniques.

When calling operator "new []" to allocate memory, it must be freed with operator "delete []".

Warning No. 2: V716 Suspicious type conversion in return statement: returned BOOL, but function actually returns HRESULT. idolphinstart.cpp 78

#define STDMETHODIMP    HRESULT STDMETHODCALLTYPE

STDMETHODIMP CDolphinSmalltalk::GetVersionInfo(LPVOID pvi)
{
  extern BOOL __stdcall GetVersionInfo(VS_FIXEDFILEINFO* ....);
  return ::GetVersionInfo(static_cast<VS_FIXEDFILEINFO*>(pvi));
}

In this code, type "BOOL" is implicitly cast to "HRESULT". Although this operation is quite valid in C++, in practice it doesn't make sense in practice. Type HRESULT is designed to store a status value, and it has a fairly complex format and has nothing in common with type BOOL.

Warning No. 3: V701 realloc() possible leak: when realloc() fails in allocating memory, original pointer 'elems' is lost. Consider assigning realloc() to a temporary pointer. compiler.cpp 2922

POTE Compiler::ParseByteArray()
{
  NextToken();
  while (m_ok && !ThisTokenIsClosing())
  {
    if (elemcount>=maxelemcount)
    {
      _ASSERTE(maxelemcount > 0);
      maxelemcount *= 2;
      elems = (BYTE*)realloc(elems, maxelemcount*sizeof(BYTE));
    }
    ....
  }
  ....
}

This code is potentially dangerous: we recommend using a separate variable to store the return result of function realloc(). The realloc() function is used to change the size of a memory block. If such change is impossible for the moment, it will return a null pointer. The problem is that pointer ptr, referring to this memory block, may get lost when using constructs like "ptr = realloc(ptr, ...)".

Two more issues of this kind:

  • V701 realloc() possible leak: when realloc() fails in allocating memory, original pointer 'm_pAllocations' is lost. Consider assigning realloc() to a temporary pointer. alloc.cpp 436
  • V701 realloc() possible leak: when realloc() fails in allocating memory, original pointer 'pUnmarked' is lost. Consider assigning realloc() to a temporary pointer. gc.cpp 217

Warning No. 4: V547 Expression 'i >= 0' is always true. Unsigned type value is always >= 0. compact.cpp 35

// Answer the index of the last occuppied OT entry
unsigned __stdcall ObjectMemory::lastOTEntry()
{
  HARDASSERT(m_pOT);
//  HARDASSERT(m_nInCritSection > 0);

  unsigned i = m_nOTSize-1;
  const OTE* pOT = m_pOT;
  while (pOT[i].isFree())
  {
    ASSERT(i >= 0);
    i--;
  }

  return i;
}

There is hardly any bug here, but the code doesn't look right anyway. Thearray items are checked in turn until the isFree() function returns false. ASSERT is incorrect here because it doesn't check anything. The 'i' variable is unsigned, so it will always be greater than or equal to 0.

One more '>=0' check over an unsigned type:

  • V547 Expression is always true. Unsigned type value is always >= 0. loadimage.cpp 343

Warning No. 5: V730 Not all members of a class are initialized inside the constructor. Consider inspecting: m_dwSize. imagefilemapping.h 13

class ImageFileMapping
{
  HANDLE m_hFile;
  HANDLE m_hMapping;
  LPVOID m_pData;
  DWORD  m_dwSize;

public:
  ImageFileMapping() : m_hFile(0), m_hMapping(0), m_pData(NULL){}
  ~ImageFileMapping() { Close(); }
  ....
};

This is just another example of potentially dangerous code. Class "ImageFileMapping" contains four fields, but only three of them are initialized in the constructor; member 'm_dwSize' remains uninitialized.

It's quite a common practice to ignore the "size" field in a class if the pointer to the array is still null. But it's easy to make a mistake in such code, so it's better to initialize all the class members.

Other similar classes:

  • V730 Not all members of a class are initialized inside the constructor. Consider inspecting: m_flags, m_oopWorkspacePools, m_context, m_compiledMethodClass. compiler.cpp 84
  • V730 Not all members of a class are initialized inside the constructor. Consider inspecting: m_tokenType, m_integer, tp, m_cc, m_base. lexer.cpp 40

Warning No. 6: V665 Possibly, the usage of '#pragma warning(default: X)' is incorrect in this context. The '#pragma warning(push/pop)' should be used instead. Check lines: 99, 101. compact.cpp 101

// Perform a compacting GC
size_t ObjectMemory::compact()
{
  ....
  #pragma warning (disable : 4127)
  while(true)
  #pragma warning (default : 4127)
  ....
}

Programmers often assume that all the warnings disabled by "pragma warning(disable: X)" will start working again after the "pragma warning(default : X)" directive. This is a wrong assumption. The 'pragma warning(default : X)' directive restores the DEFAULT state of warning 'X'. And it's obviously a different thing.

Fixed version of the code:

size_t ObjectMemory::compact()
{
  ....
  #pragma warning(push)
  #pragma warning (disable : 4127)
  while(true)
  #pragma warning(pop)
  ....
}

Here's an interesting article on the subject: "So you want to suppress this message in Visual C++".

And here is a list of other issues of this kind:

  • V665 Possibly, the usage of '#pragma warning(default: X)' is incorrect in this context. The '#pragma warning(push/pop)' should be used instead. Check lines: 244, 246. expire.cpp 246
  • V665 Possibly, the usage of '#pragma warning(default: X)' is incorrect in this context. The '#pragma warning(push/pop)' should be used instead. Check lines: 226, 241. expire.cpp 241
  • V665 Possibly, the usage of '#pragma warning(default: X)' is incorrect in this context. The '#pragma warning(push/pop)' should be used instead. Check lines: 126, 128. interfac.cpp 128
  • V665 Possibly, the usage of '#pragma warning(default: X)' is incorrect in this context. The '#pragma warning(push/pop)' should be used instead. Check lines: 385, 387. interprt.cpp 387

Warning No. 7: V576 Incorrect format. Consider checking the fourth actual argument of the 'wsprintfA' function. To print the value of pointer the '%p' should be used. interfac.cpp 679

inline DWORD __stdcall
Interpreter::GenericCallbackMain(SMALLINTEGER id, BYTE* lpArgs)
{
  ....
#ifdef _DEBUG
  {
    char buf[128];
    wsprintf(buf, "WARNING: .... (%d, %x)\n", id, lpArgs);
    WarningWithStackTrace(buf);
  }
  #endif
  ....
}

Programmers often use specifier '%x' to print a pointer's value.

However, it's a mistake because this code will work only on the systems where the pointer size coincides with the size of type 'int'. On Win64, for example, this code will print only the least significant part of pointer 'ptr'. To avoid this bug, one should use specifier '%p'.

Warning No. 8: V547 Expression 'ch > 127' is always false. The value range of char type: [-128, 127]. decode.cpp 55

ostream& operator<<(ostream& stream, const VariantCharOTE* oteChars)
{
  ....
  char ch = string->m_characters[i];
  //if (ch = '\0') break;
  if (ch < 32 || ch > 127)  // <=
  {
    static char hexChars[16+1] = "0123456789ABCDEF";
    ....
  }
  ....
}

The 'char' type's default range is [-127;127]. We can use the /J compilation switch to make the compiler use the range [0;255] instead. However, there is no such switch specified when compiling this source file, so the check "ch > 127" doesn't make sense.

Warning No. 9: V688 The 'prev' function argument possesses the same name as one of the class members, which can result in a confusion. thrdcall.h 126

void LinkAfter(T* prev)
{
  T* pThis = static_cast<T*>(this);
  this->next = prev->next;
  if (this->next)
    this->next->prev = pThis;
  this->prev = prev;
  prev->next = pThis;
}

I don't think there is any bug in this function, but it's not a good style to give the same name to class function parameters and class members as it may lead to typos that will result in handling a wrong variable.

Warning No. 10: V601 The 'false' value is implicitly cast to the integer type. compiler.cpp 1940

int Compiler::ParseUnaryContinuation(...., int textPosition)
{
  int continuationPointer = m_codePointer;
  MaybePatchLiteralMessage();
  while (m_ok && (ThisToken()==NameConst)) 
  {
    int specialCase=false;  // <=
    ....
    if (!specialCase)       // <=
    {
      int sendIP = GenMessage(ThisTokenText(), 0, textPosition);
      AddTextMap(sendIP, textPosition, ThisTokenRange().m_stop);
    }
    ....
  }
  ....
}

The warning for this code should rather be treated as a recommendation. If 'specialCase' is handled as a logical variable throughout the code, it is better to use the standard type 'bool' as its type.

Conclusion

So, one more project has been added to the list of open-source projects scanned by our analyzer.

Preparing articles with project analysis results, we skip a lot of warnings issued by the analyzer. That's why we recommend the project authors to scan their code and examine all the warnings themselves.

As a usual reminder, remember that it is regular, not occasional, use that makes the analyzer valuable.