From: Jean-Marc Lasgouttes Date: Fri, 13 Sep 2019 20:36:53 +0000 (+0200) Subject: Various fixes suggested by cppcheck X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=351bc30def986d1c408e0ee2a97028b2f50b8dae;p=features.git Various fixes suggested by cppcheck Rename local variables the hide other ones: get_binary_path, find_python_binary Use unsigned int for conversion from hex using sscanf(). FileName::checksum(), parsecmd (SystemCall): use explcit values rather than variable which value is known. --- diff --git a/src/support/FileName.cpp b/src/support/FileName.cpp index 7c47de0948..42956b05b6 100644 --- a/src/support/FileName.cpp +++ b/src/support/FileName.cpp @@ -548,16 +548,14 @@ unsigned long checksum_ifstream_fallback(char const * file) unsigned long FileName::checksum() const { - unsigned long result = 0; - if (!exists()) { //LYXERR0("File \"" << absFileName() << "\" does not exist!"); - return result; + return 0; } // a directory may be passed here so we need to test it. (bug 3622) if (isDirectory()) { LYXERR0('"' << absFileName() << "\" is a directory!"); - return result; + return 0; } // This is used in the debug output at the end of the method. @@ -565,6 +563,8 @@ unsigned long FileName::checksum() const if (lyxerr.debugging(Debug::FILES)) t.restart(); + unsigned long result = 0; + #if QT_VERSION >= 0x999999 // First version of checksum uses Qt4.4 mmap support. // FIXME: This code is not ready with Qt4.4.2, @@ -574,7 +574,7 @@ unsigned long FileName::checksum() const // QAbstractFileEngine::MapExtension) QFile qf(fi.filePath()); if (!qf.open(QIODevice::ReadOnly)) - return result; + return 0; qint64 size = fi.size(); uchar * ubeg = qf.map(0, size); uchar * uend = ubeg + size; @@ -594,7 +594,7 @@ unsigned long FileName::checksum() const int fd = open(file, O_RDONLY); if (!fd) - return result; + return 0; struct stat info; if (fstat(fd, &info)){ @@ -608,7 +608,7 @@ unsigned long FileName::checksum() const // Some platforms have the wrong type for MAP_FAILED (compaq cxx). if (mm == reinterpret_cast(MAP_FAILED)) { close(fd); - return result; + return 0; } char * beg = static_cast(mm); diff --git a/src/support/Package.cpp b/src/support/Package.cpp index 284b5689da..f931ddf705 100644 --- a/src/support/Package.cpp +++ b/src/support/Package.cpp @@ -508,9 +508,9 @@ FileName const get_binary_path(string const & exe) // This will do nothing if *it is already absolute. string const exe_dir = makeAbsPath(*it).absFileName(); - FileName const exe_path(addName(exe_dir, exe_name)); - if (exe_path.exists()) - return exe_path; + FileName const exe_path2(addName(exe_dir, exe_name)); + if (exe_path2.exists()) + return exe_path2; } // Didn't find anything. diff --git a/src/support/Systemcall.cpp b/src/support/Systemcall.cpp index ae224cbf8c..a270b4118d 100644 --- a/src/support/Systemcall.cpp +++ b/src/support/Systemcall.cpp @@ -205,7 +205,7 @@ string const parsecmd(string const & incmd, string & infile, string & outfile, in_double_quote = !in_double_quote; } } else if (c == '\\' && !escaped) { - escaped = !escaped; + escaped = true; } else if (c == '>' && !(in_double_quote || escaped)) { if (suffixIs(outcmd[o], " 2")) { outcmd[o] = rtrim(outcmd[o], "2"); diff --git a/src/support/lstrings.cpp b/src/support/lstrings.cpp index e2f3d9c084..55e66ce077 100644 --- a/src/support/lstrings.cpp +++ b/src/support/lstrings.cpp @@ -422,10 +422,10 @@ bool isHex(docstring const & str) } -int hexToInt(docstring const & str) +unsigned int hexToInt(docstring const & str) { string s = to_ascii(str); - int h; + unsigned int h; sscanf(s.c_str(), "%x", &h); return h; } diff --git a/src/support/lstrings.h b/src/support/lstrings.h index ff0ce1d207..f661fc5b85 100644 --- a/src/support/lstrings.h +++ b/src/support/lstrings.h @@ -53,7 +53,7 @@ bool isHexChar(char_type); bool isHex(docstring const & str); -int hexToInt(docstring const & str); +unsigned int hexToInt(docstring const & str); /// is \p str pure ascii? bool isAscii(docstring const & str); diff --git a/src/support/os.cpp b/src/support/os.cpp index ab9d4159a6..10430bf5a2 100644 --- a/src/support/os.cpp +++ b/src/support/os.cpp @@ -99,15 +99,15 @@ static string const find_python_binary() // but we are trying hard to find a valid python binary vector const path = getEnvPath("PATH"); lyxerr << "Looking for python 3.x ...\n"; - for (auto bin: path) { + for (auto bin : path) { QString const dir = toqstr(bin); string const localdir = dir.toLocal8Bit().constData(); QDir qdir(dir); qdir.setFilter(QDir::Files | QDir::Executable); QStringList list = qdir.entryList(QStringList("python3*")); - for (auto bin: list) { + for (auto bin2 : list) { string const binary = addName(localdir, - bin.toLocal8Bit().constData()); + bin2.toLocal8Bit().constData()); command = python23_call(binary, true); if (!command.empty()) return command; @@ -123,15 +123,15 @@ static string const find_python_binary() // the search is probably broader than required // but we are trying hard to find a valid python binary lyxerr << "Looking for python 2.x ...\n"; - for (auto bin: path) { + for (auto bin : path) { QString const dir = toqstr(bin); string const localdir = dir.toLocal8Bit().constData(); QDir qdir(dir); qdir.setFilter(QDir::Files | QDir::Executable); QStringList list = qdir.entryList(QStringList("python2*")); - for (auto bin: list) { + for (auto bin2 : list) { string const binary = addName(localdir, - bin.toLocal8Bit().constData()); + bin2.toLocal8Bit().constData()); command = python23_call(binary, true); if (!command.empty()) return command;