Examples of errors detected by the V562 diagnostic.


V562. It's odd to compare a bool type value with a value of N.


Spvolren

V562 It's odd to compare a bool type value with a value of -1: !(fp = fopen (filename, "wb")) == - 1. spvolren ppm.c 118


void ppmWrite(char *filename, PPMFile *ppmFile)
{
  ...
  FILE *fp;
  if (! (fp = fopen(filename, "wb")) == -1) {
    perror("opening image file failed");
    exit(1);
  }
  ...
}

ReactOS

V562 It's odd to compare 0 or 1 with a value of 4294967295. eventlog file.c 264


#define INVALID_SET_FILE_POINTER ((DWORD)-1)

BOOL LogfInitializeExisting(PLOGFILE LogFile)
{
  ...
  if (!SetFilePointer(LogFile->hFile, 0, NULL, FILE_BEGIN) ==
      INVALID_SET_FILE_POINTER)
  {
      DPRINT1("SetFilePointer() failed! %d\n", GetLastError());
      return FALSE;
  }
  ...
}

This is what should have been written here: if (SetFilePointer(LogFile->hFile, 0, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER)


Blender

V562 It's odd to compare 0 or 1 with a value of 1: (base->object->actcol > 0) > (1). bf_editor_render render_preview.c 361


#define MAX2(x,y) ( (x)>(y) ? (x) : (y) )

static Scene *preview_prepare_scene(....)
{
  ...
  int actcol = MAX2(base->object->actcol > 0, 1) - 1;
  ...
}

Most likely this is what should be written here: int actcol = MAX2(base->object->actcol, 1) - 1;


Windows 8 Driver Samples

V562 It's odd to compare 0 or 1 with a value of 5: 0 < dwMaskSize < 5. nic_pm.c 825


VOID MPCreateProgrammableFilter(....)
{
  ....
  ASSERT (0 < dwMaskSize <5);
  ....
}

Haiku Operation System

V562 It's odd to compare 0 or 1 with a value of 0. cmp.c 300


#ifndef same_file
# define same_file(s, t) \
    ((((s)->st_ino == (t)->st_ino) \
     && ((s)->st_dev == (t)->st_dev)) \
     || same_special_file (s, t))
#endif

int
main (int argc, char **argv)
{
  ....
  if (0 < same_file (&stat_buf[0], &stat_buf[1])           // <=
      && same_file_attributes (&stat_buf[0], &stat_buf[1])
      && file_position (0) == file_position (1))
    return EXIT_SUCCESS;
  ....
}

Similar errors can be found in some other places:

  • V562 It's odd to compare 0 or 1 with a value of 0. cmp.c 313

Haiku Operation System

V562 It's odd to compare a bool type value with a value of 18: 0x12 == IsProfessionalSpdif(). CEchoGals_mixer.cpp 533


#define ECHOSTATUS_DSP_DEAD 0x12                          // <=

virtual BOOL IsProfessionalSpdif()                        // <=
{
  ....
  return( (....) ? TRUE : FALSE );
}

ECHOSTATUS CEchoGals::ProcessMixerFunction
(
  PMIXER_FUNCTION  pMixerFunction,
  INT32 &          iRtnDataSz
)
{
  ....
  case MXF_GET_PROF_SPDIF :
      if ( ECHOSTATUS_DSP_DEAD == IsProfessionalSpdif() ) // <=
      {
        Status = ECHOSTATUS_DSP_DEAD;
      }
      else
      {
        pMixerFunction->Data.bProfSpdif = IsProfessionalSpdif();
      }
  ....
}


Do you make errors in the code?

Check your code
with PVS-Studio

Static code analysis
for C, C++ and C#

goto PVS-Studio;