Examples of errors detected by the V561 diagnostic.


V561. It's probably better to assign value to 'foo' variable than to declare it anew.


FCEUX

V561 It's probably better to assign value to 'x' variable than to declare it anew. Previous daclaration: ines.cpp, line 960. fceux ines.cpp 962


int iNesSaveAs(char* name)
{
  ...
  fp = fopen(name,"wb");
  int x = 0;
  if (!fp)
    int x = 1;
  ...
}

Vscap

V561 It's probably better to assign value to 'ret' variable than to declare it anew. Previous daclaration: TransparentWnd.cpp, line 1802. vscap transparentwnd.cpp 1804


BOOL CTransparentWnd::SaveShape(FILE* fptr)
{
  ...
  BOOL ret = TRUE;
  if (m_hbitmap)
    BOOL ret = picture.SaveToFile(fptr);
  ...
}

FlightGear

V561 It's probably better to assign value to 's' variable than to declare it anew. Previous declaration: dclgps.cxx, line 480. dclgps.cxx 485


void DCLGPS::update(double dt) {
  ....
  string s;
  if(fgGetBool("/instrumentation/nav[0]/slaved-to-gps")) {
    // TODO - avoid the hardwiring on nav[0]
    s = "Adj Nav Crs to ";
  } else {
    string s = "GPS Course is ";
  }
  ....
}

Unreal Engine 4

V561 It's probably better to assign value to 'Existing' variable than to declare it anew. Previous declaration: streamablemanager.cpp, line 325. streamablemanager.cpp 332


void FStreamableManager::AsyncLoadCallback(
  FStringAssetReference Request)
{
  ....
  FStreamable* Existing = StreamableItems.FindRef(TargetName);
  ....
  if (!Existing)
  {
    // hmm, maybe it was redirected by a consolidate
    TargetName = ResolveRedirects(TargetName);
    FStreamable* Existing = StreamableItems.FindRef(TargetName);
  }
  if (Existing && Existing->bAsyncLoadRequestOutstanding)
  ....
}

This is what should have been written here: Existing = StreamableItems.FindRef(TargetName);


Oracle VM Virtual Box

V561 It's probably better to assign value to 'Status' variable than to declare it anew. Previous declaration: vboxmpwddm.cpp, line 5723. vboxmpwddm.cpp 5728


static NTSTATUS APIENTRY
DxgkDdiRenderNew(CONST HANDLE hContext, DXGKARG_RENDER  *pRender)
{
  ....
  NTSTATUS Status = STATUS_SUCCESS;    // <=

  __try
  {
    ....
    NTSTATUS Status = STATUS_SUCCESS;  // <=
    ....
  }
  __except (EXCEPTION_EXECUTE_HANDLER)
  {
    Status = STATUS_INVALID_PARAMETER;
    WARN(("invalid parameter"));
  }

  return Status;
}

Any change of the variable within the try..except section won't change the returned value and the external (in relation to the try {} block) variable will be changed only if an exception occurs.


Godot Engine

V561 It's probably better to assign value to 'styles_count' variable than to declare it anew. Previous declaration: export.cpp, line 610. export.cpp 633


void EditorExportPlatformAndroid::_fix_manifest(....)
{
  ....
  uint32_t string_count;
  uint32_t styles_count;
  uint32_t string_flags;
  uint32_t string_data_offset;
  ....
  switch(chunk)
  {
    case CHUNK_STRINGS:
    {
      int iofs=ofs+8;
      uint32_t string_count=decode_uint32(....);
      uint32_t styles_count=decode_uint32(....);
      uint32_t string_flags=decode_uint32(....);
      uint32_t string_data_offset=decode_uint32(....);
      uint32_t styles_offset=decode_uint32(....);
      ....
    }
    ....
  }
  ....
}

Godot Engine

V561 It's probably better to assign value to 'all_const' variable than to declare it anew. Previous declaration: shader_language.cpp, line 1225. shader_language.cpp 1274


ShaderLanguage::Node*
ShaderLanguage::validate_function_call(....)
{
  ....
  bool all_const=true;
  ....
  if (p_func->op==OP_CONSTRUCT && all_const)
  {
    bool all_const=false;
    ....
  }
  ....
}

Telegram

V561 It's probably better to assign value to 'assocHandlers' variable than to declare it anew. Previous declaration: pspecific_wnd.cpp, line 2031. Telegram pspecific_wnd.cpp 2107


bool psShowOpenWithMenu(....)
{
  ....
  IEnumAssocHandlers *assocHandlers = 0;
  ....
  if (....)
  {
    ....
    IEnumAssocHandlers *assocHandlers = 0;
    ....
  }
  ....
}

Similar errors can be found in some other places:

  • V561 It's probably better to assign value to 'ms' variable than to declare it anew. Previous declaration: window.cpp, line 1371. Telegram window.cpp 1467

Firebird

V561 It's probably better to assign value to 'exit_code' variable than to declare it anew. Previous declaration: svc.cpp, line 1893. svc.cpp 1898


THREAD_ENTRY_DECLARE Service::run(THREAD_ENTRY_PARAM arg)
{
  int exit_code = -1;
  try
  {
    Service* svc = (Service*)arg;
    RefPtr<SvcMutex> ref(svc->svc_existence);
    int exit_code = svc->svc_service_run->serv_thd(svc);

    svc->started();
    svc->svc_sem_full.release();
    svc->finish(SVC_finished);
  }
  catch (const Exception& ex)
  {
    // Not much we can do here
    iscLogException("Exception in Service::run():", ex);
  }

  return (THREAD_ENTRY_RETURN)(IPTR) exit_code;
}

OpenJDK

V561 It's probably better to assign value to 'maybe_null' variable than to declare it anew. Previous declaration: graphKit.cpp, line 2170. graphKit.cpp 2175


Node* GraphKit::record_profiled_receiver_for_speculation(Node* n)
{
  ....
  ciKlass* exact_kls = profile_has_unique_klass();
  bool maybe_null = true;
  if (java_bc() == Bytecodes::_checkcast ||
      java_bc() == Bytecodes::_instanceof ||
      java_bc() == Bytecodes::_aastore) {
    ciProfileData* data =
      method()->method_data()->bci_to_data(bci());
    bool maybe_null = data == NULL ? true :    // <=
                      data->as_BitData()->null_seen();
  }
  return record_profile_for_speculation(n,
    exact_kls, maybe_null);
  return n;
}


Do you make errors in the code?

Check your code
with PVS-Studio

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

goto PVS-Studio;