Repository: xbmc/xbmc
File: xbmc/filesystem/NFSDirectory.cpp (C++)

Code before:
}

bool CNFSDirectory::GetDirectory(const CURL& url, CFileItemList &items)
{
  // We accept nfs://server/path[/file]]]]
  std::unique_lock lock(gNfsConnection);

  std::string strDirName = "";
  if (!gNfsConnection.Connect(url, strDirName))
  {
    //connect has failed - so try to get the exported filesystems if no path is given to the url
    if (url.GetShareName().empty())
    {
      if (url.GetHostName().empty())
        return GetServerList(items);
      else
      {
        std::string myStrPath(url.Get());
        URIUtils::AddSlashAtEnd(myStrPath); //be sure the dir ends with a slash
        return GetDirectoryFromExportList(myStrPath, items);
      }
    }
    return false;
  }

  struct nfsdir* nfsdir = NULL;
  if (nfs_opendir(gNfsConnection.GetNfsContext(), strDirName.c_str(), &nfsdir) != 0)
  {
    CLog::Log(LOGERROR, "Failed to open({}) {}", strDirName,
              nfs_get_error(gNfsConnection.GetNfsContext()));
    return false;
  }
  lock.unlock();

  const std::vector<std::string> exclusions = {".", "..", "lost+found"};
  const auto notExcluded = exclusions.end();

  std::string myStrPath(url.Get());
  URIUtils::AddSlashAtEnd(myStrPath);
  URIUtils::AddSlashAtEnd(strDirName);

  std::string resolvedPath;
  std::vector<CFileItemPtr> fileItems;
  struct nfsdirent* dirent = NULL;
  while ((dirent = nfs_readdir(gNfsConnection.GetNfsContext(), nfsdir)) != NULL)
  {
    const std::string& strName = dirent->name;

    //resolve symlinks
    //resolve symlink changes dirent and strName
    const bool isSymLink = dirent->type == NF3LNK;

Review comment (bug):
```suggestion
  struct nfsdir* nfsdir = nullptr;
```

Code after:

bool CNFSDirectory::GetDirectory(const CURL& url, CFileItemList &items)
{
  // We accept nfs://server/path[/file]]]]
  std::unique_lock lock(gNfsConnection);

  std::string strDirName = "";
  if (!gNfsConnection.Connect(url, strDirName))
  {
    //connect has failed - so try to get the exported filesystems if no path is given to the url
    if (url.GetShareName().empty())
    {
      std::vector<CFileItemPtr> fileItems =
          url.GetHostName().empty() ? GetServerList() : GetDirectoryFromExportList(url);
      bool ret = !fileItems.empty();
      items.AddItems(std::move(fileItems));
      return ret;
    }
    return false;
  }

  struct nfsdir* nfsdir = nullptr;
  if (nfs_opendir(gNfsConnection.GetNfsContext(), strDirName.c_str(), &nfsdir) != 0)
  {
    CLog::Log(LOGERROR, "Failed to open({}) {}", strDirName,
              nfs_get_error(gNfsConnection.GetNfsContext()));
    return false;
  }
  lock.unlock();

  std::string myStrPath(url.Get());
  URIUtils::AddSlashAtEnd(myStrPath);
  URIUtils::AddSlashAtEnd(strDirName);

  std::string resolvedPath;
  std::vector<CFileItemPtr> fileItems;
  struct nfsdirent* dirent = nullptr;
  while ((dirent = nfs_readdir(gNfsConnection.GetNfsContext(), nfsdir)) != nullptr)
  {
    const std::string& name = dirent->name;

    //resolve symlinks
    //resolve symlink changes dirent and name
    const bool isSymLink = dirent->type == NF3LNK;
    if (isSymLink && !ResolveSymlink(strDirName, dirent, resolvedPath))
      continue;

    if (name == "." || name == ".." || name == "lost+found")
      continue;

    const bool isDir = dirent->type == NF3DIR;