Skip to content

Commit

Permalink
fix #661 CodeQL new warnings (#664)
Browse files Browse the repository at this point in the history
* fix #661 CodeQL new warnings

* fix codacy warnings detected after CodeQL fix

* limit cin width

* indent

* unusued variables
  • Loading branch information
Nelson-numerical-software committed Jun 6, 2022
1 parent cf1adbb commit 1058dbe
Show file tree
Hide file tree
Showing 31 changed files with 68 additions and 79 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -20,6 +20,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

- [#659](http://github.com/Nelson-numerical-software/nelson/issues/659): qhelpgenerator on macOs after brew Qt6.3.0 update.

- [#661](http://github.com/Nelson-numerical-software/nelson/issues/661): CodeQL warnings.

## 0.6.5 (2022-05-26)

### Added
Expand Down
2 changes: 1 addition & 1 deletion modules/core/src/cpp/GetNelsonPath.cpp
Expand Up @@ -67,7 +67,7 @@ get_basepathU()
{
std::string path = "";
pid_t pid = getpid();
char buf[10];
char buf[13];
sprintf(buf, "%d", pid);
std::string _link = "/proc/";
_link.append(buf);
Expand Down
4 changes: 2 additions & 2 deletions modules/engine/src/cpp/StartNelson.cpp
Expand Up @@ -111,7 +111,7 @@ ErrorPathDetection(NELSON_ENGINE_MODE _mode)
}
#else
msg = msg + L"\n";
fwprintf(stderr, msg.c_str());
fwprintf(stderr, L"%ls", msg.c_str());
#endif
}
//=============================================================================
Expand All @@ -128,7 +128,7 @@ ErrorInterpreter(NELSON_ENGINE_MODE _mode)
}
#else
msg = msg + L"\n";
fwprintf(stderr, msg.c_str());
fwprintf(stderr, L"%ls", msg.c_str());
#endif
}
//=============================================================================
Expand Down
2 changes: 1 addition & 1 deletion modules/engine/src/cpp/StartNelsonUserModules.cpp
Expand Up @@ -25,7 +25,7 @@ printError(Interface* io, const std::wstring& message)
io->warningMessage(message);
} else {
const wchar_t* format = L"%s\n";
fwprintf(stdout, format, message.c_str()); // lgtm [cpp/wrong-type-format-argument]
fwprintf(stdout, format, message.c_str());
}
}
//=============================================================================
Expand Down
3 changes: 1 addition & 2 deletions modules/engine/src/cpp/StartNelsonUserScript.cpp
Expand Up @@ -39,8 +39,7 @@ StartNelsonUserScript(Evaluator* eval)
io->errorMessage(errmsg);
} else {
const wchar_t* format = L"%s\n";
fwprintf(
stderr, format, errmsg.c_str()); // lgtm [cpp/wrong-type-format-argument]
fwprintf(stderr, format, errmsg.c_str());
}
}
return true;
Expand Down
12 changes: 3 additions & 9 deletions modules/file_archiver/src/c/minizip/mz_zip.c
Expand Up @@ -141,13 +141,7 @@ mz_zip_search_eocd(void* stream, int64_t* central_pos)
return err;

file_size = mz_stream_tell(stream);

if (max_back <= 0) { // lgtm [cpp/constant-comparison]
max_back = file_size;
}
if (max_back > file_size) {
max_back = file_size;
}
max_back = file_size;
return mz_stream_find_reverse(stream, (const void*)find, sizeof(find), max_back, central_pos);
}

Expand Down Expand Up @@ -1397,11 +1391,11 @@ mz_zip_close(void* handle)
}

if (zip->file_info_stream != NULL) {
mz_stream_mem_close(zip->file_info_stream); // lgtm [cpp/useless-expression]
mz_stream_mem_close(zip->file_info_stream);
mz_stream_mem_delete(&zip->file_info_stream);
}
if (zip->local_file_info_stream != NULL) {
mz_stream_mem_close(zip->local_file_info_stream); // lgtm [cpp/useless-expression]
mz_stream_mem_close(zip->local_file_info_stream);
mz_stream_mem_delete(&zip->local_file_info_stream);
}

Expand Down
4 changes: 2 additions & 2 deletions modules/files_folders_functions/src/cpp/ListFiles.cpp
Expand Up @@ -178,7 +178,7 @@ ListFiles(const std::wstring& directory, bool bSubdirectories)
struct
{
bool
operator()(FileInfo a, FileInfo b) // lgtm [cpp/large-parameter]
operator()(FileInfo a, FileInfo b)
{
return static_cast<int>(a.isDir()) > static_cast<int>(b.isDir());
}
Expand All @@ -189,7 +189,7 @@ ListFiles(const std::wstring& directory, bool bSubdirectories)
struct
{
bool
operator()(FileInfo a, FileInfo b) // lgtm [cpp/large-parameter]
operator()(FileInfo a, FileInfo b)
{
return a.getName() < b.getName();
}
Expand Down
2 changes: 1 addition & 1 deletion modules/help_browser/src/cpp/HelpBrowser.cpp
Expand Up @@ -140,7 +140,7 @@ HelpBrowser::closeBrowser()
}
}
//=============================================================================
HelpBrowser::HelpBrowser() { }
HelpBrowser::HelpBrowser() {}
//=============================================================================
void
HelpBrowser::registerHelpFiles(const wstringVector& filenames)
Expand Down
7 changes: 6 additions & 1 deletion modules/help_browser/src/cpp/HelpCollection.cpp
Expand Up @@ -51,7 +51,12 @@ HelpCollection::HelpCollection()
std::wstring cachedCollectionFile = getNelsonCachedCollectionFullFilename();
if (!IsFile(cachedCollectionFile)) {
std::wstring collectionFile = getNelsonCollectionFullFilename();
QHelpEngineCore* collection = new QHelpEngineCore(wstringToQString(collectionFile));
QHelpEngineCore* collection = nullptr;
try {
collection = new QHelpEngineCore(wstringToQString(collectionFile));
} catch (std::bad_alloc&) {
collection = nullptr;
}
if (collection) {
collection->copyCollectionFile(wstringToQString(cachedCollectionFile));
delete collection;
Expand Down
2 changes: 1 addition & 1 deletion modules/help_tools/src/c/hoedown/document.c
Expand Up @@ -1191,7 +1191,7 @@ char_link(hoedown_buffer* ob, hoedown_document* doc, uint8_t* data, size_t offse
}
i++;
} else if (i >= 1 && _isspace(data[i - 1])
&& (data[i] == '\'' || data[i] == '"')) // lgtm [cpp/constant-comparison]
&& (data[i] == '\'' || data[i] == '"'))
{
break;
} else {
Expand Down
2 changes: 1 addition & 1 deletion modules/help_tools/src/c/hoedown/html.c
Expand Up @@ -389,7 +389,7 @@ rndr_raw_block(hoedown_buffer *ob, const hoedown_buffer *text, const hoedown_ren
{
return;
}
/* FIXME: Do we *really* need to trim the HTML? How does that make a difference? */ // lgtm [cpp/fixme-comment]
/* FIXME: Do we *really* need to trim the HTML? How does that make a difference? */
sz = text->size;
while (sz > 0 && text->data[sz - 1] == '\n')
{
Expand Down
4 changes: 2 additions & 2 deletions modules/help_tools/src/cpp/XmlDocDocument.cpp
Expand Up @@ -790,14 +790,14 @@ XmlDocDocument::writeAsMarkdown(std::string& utf8stream)
if (haveExample()) {
XmlDocGenericItem* pItem = findfirst(EXAMPLES_TAG);
XmlDocExamples* pExamples = (XmlDocExamples*)pItem;
pExamples->writeHeaderAsMarkdown(utf8stream); // lgtm [cpp/useless-expression] //-V522
pExamples->writeHeaderAsMarkdown(utf8stream);
}
// header
if (isKeywordDocument()) {
XmlDocGenericItem* pItem = findfirst(KEYWORD_TAG);
if (pItem) {
XmlDocKeywordItem* pItemKeyword = (XmlDocKeywordItem*)pItem;
pItemKeyword->writeHeaderAsMarkdown(utf8stream); // lgtm [cpp/useless-expression]
pItemKeyword->writeHeaderAsMarkdown(utf8stream);
}
}
if (isChapterDocument()) {
Expand Down
6 changes: 1 addition & 5 deletions modules/interpreter/src/cpp/Context.cpp
Expand Up @@ -59,11 +59,7 @@ Context::pushScope(const std::string& name)
} catch (const std::bad_alloc&) {
Error(ERROR_STACK_DEPTH_EXCEEDED);
}
if (sc != nullptr) {
scopestack.push_back(sc);
} else {
Error(ERROR_STACK_DEPTH_EXCEEDED);
}
scopestack.push_back(sc);
}
//=============================================================================
void
Expand Down
5 changes: 2 additions & 3 deletions modules/interpreter/src/cpp/NelsonLexer.cpp
Expand Up @@ -865,9 +865,8 @@ lexInitialState()
{
if (isNewline() != 0) {
NextLine();
} else if (isWhitespace() != 0) { // lgtm [cpp/empty-block]
// nothing
} else if (match(";") != 0) { // lgtm [cpp/empty-block]
} else if (isWhitespace() != 0) { // nothing
} else if (match(";") != 0) {
// nothing
} else if (currentChar() == '%') {
fetchComment();
Expand Down
5 changes: 2 additions & 3 deletions modules/memory_manager/src/cpp/MemoryInformation.cpp
Expand Up @@ -174,9 +174,8 @@ getTotalVirtualMemoryByNelson()
int utime, stime, cutime, cstime, counter, priority, timeout;
unsigned itrealvalue, starttime;
int vsize = 0, rss = 0;
f >> pid >> name >> state >> ppid >> pgrp >> session >> tty // lgtm [cpp/dangerous-cin]
>> tpgid // lgtm [cpp/dangerous-cin]
>> flags; // lgtm [cpp/dangerous-cin]
std::cin.width(4096);
f >> pid >> name >> state >> ppid >> pgrp >> session >> tty >> tpgid >> flags;
f >> minflt >> cminflt >> majflt >> cmajflt >> utime >> stime >> cutime;
f >> cstime >> counter >> priority >> timeout >> itrealvalue >> starttime;
f >> vsize >> rss;
Expand Down
2 changes: 1 addition & 1 deletion modules/mex/src/cpp/MxStruct.cpp
Expand Up @@ -102,7 +102,7 @@ mxGetFieldByNumber(const mxArray* pm, mwIndex index, int fieldnumber)
if (!mxIsStruct(pm)) {
return nullptr;
}
if (index >= mxGetNumberOfElements(pm) || index < 0) { // lgtm [cpp/constant-comparison]
if (index >= mxGetNumberOfElements(pm) || index < 0) {
return nullptr;
}
if (fieldnumber >= mxGetNumberOfFields(pm) || fieldnumber < 0) {
Expand Down
3 changes: 1 addition & 2 deletions modules/modules_manager/builtin/cpp/ismoduleBuiltin.cpp
Expand Up @@ -20,8 +20,7 @@ Nelson::ModulesManagerGateway::ismoduleBuiltin(int nLhs, const ArrayOfVector& ar
nargincheck(argIn, 1, 1);
nargoutcheck(nLhs, 0, 1);
std::wstring moduleshortname;
if (argIn[0].isRowVectorCharacterArray() ||
(argIn[0].isStringArray() && argIn[0].isScalar())) {
if (argIn[0].isRowVectorCharacterArray() || (argIn[0].isStringArray() && argIn[0].isScalar())) {
moduleshortname = argIn[0].getContentAsWideString();
} else {
Error(ERROR_WRONG_ARGUMENT_1_TYPE_STRING_EXPECTED);
Expand Down
4 changes: 2 additions & 2 deletions modules/modules_manager/src/c/semver/semver.c
Expand Up @@ -53,7 +53,7 @@ strcut (char *str, int begin, int len) {
size_t l;
l = strlen(str);

if((int)l < 0 || (int)l > MAX_SAFE_INT) return -1; // lgtm [cpp/constant-comparison]
if((int)l < 0 || (int)l > MAX_SAFE_INT) return -1;

if (len < 0) len = l - begin + 1;
if (begin + len > (int)l) len = l - begin;
Expand Down Expand Up @@ -97,7 +97,7 @@ parse_int (const char *s) {
if (valid == 0) return -1;

num = strtol(s, NULL, 10);
if (num > MAX_SAFE_INT) return -1; // lgtm [cpp/constant-comparison]
if (num > MAX_SAFE_INT) return -1;

return num;
}
Expand Down
2 changes: 1 addition & 1 deletion modules/operators/src/cpp/Colon.cpp
Expand Up @@ -34,7 +34,7 @@ integer_colon(NelsonType destinationClass, T low, T high, T step)
}
if (low < high) {
if (isSignedInteger(destinationClass)) {
if (step < 0) { // lgtm [cpp/constant-comparison]
if (step < 0) {
ArrayOf res = ArrayOf::emptyConstructor(1, 0);
res.promoteType(destinationClass);
return res;
Expand Down
6 changes: 3 additions & 3 deletions modules/profiler/resources/highlight.pack.js
Expand Up @@ -165,14 +165,14 @@
[],
t.c.map(function(e) {
return (
(n = "self" === e ? t : e).v && // lgtm [js/use-before-declaration]
(n = "self" === e ? t : e).v &&
!n.cached_variants &&
(n.cached_variants = n.v.map(function(e) {
return l(n, { v: null }, e);
})),
n.cached_variants || (n.eW && [l(n)]) || [n]
);
var n; // lgtm [js/unreachable-statement]
var n;
})
)),
t.c.forEach(function(e) {
Expand Down Expand Up @@ -430,7 +430,7 @@
" " +
e.nodeName +
'="' +
_(e.value).replace('"', "&quot;") + // lgtm [js/incomplete-sanitization]
_(e.value).replace('"', "&quot;") +
'"'
);
})
Expand Down
2 changes: 1 addition & 1 deletion modules/profiler/resources/sort.js
Expand Up @@ -25,7 +25,7 @@ function SortTable() {
arrayOfRows[i].oldIndex = i;
var celltext = rows[i]
.getElementsByTagName("td")
[sortColumn].innerHTML.replace(/<[^>]*>/g, ""); // lgtm [js/incomplete-multi-character-sanitization]
[sortColumn].innerHTML.replace(/<[^>]*>/g, "");
var re = type == "N" ? /[^\.\-\+\d]/g : /[^a-zA-Z0-9]/g;
arrayOfRows[i].value = celltext
.replace(re, "")
Expand Down
4 changes: 4 additions & 0 deletions modules/stream_manager/builtin/cpp/fwriteBuiltin.cpp
Expand Up @@ -62,6 +62,10 @@ fwriteBuiltinFiveRhs(int nLhs, const ArrayOfVector& argIn)
Error(_W("Cannot write sparse type."));
}
auto* fm = static_cast<FilesManager*>(NelsonConfiguration::getInstance()->getFileManager());
if (fm == nullptr) {
Error(_W("Problem with file manager."));
return retval;
}
auto iValue = static_cast<int32>(param1.getContentAsDoubleScalar());
if (fm->isOpened(iValue)) {
File* f = fm->getFile(iValue);
Expand Down
2 changes: 1 addition & 1 deletion modules/stream_manager/src/cpp/FileWrite.cpp
Expand Up @@ -67,7 +67,7 @@ FileWrite(File* fp, ArrayOf src, NelsonType destClass, size_t skip, bool bIsLitt
FILE* filepointer = static_cast<FILE*>(fp->getFilePointer());
if (filepointer) {
if (skip) {
char* skipdata;
char* skipdata = nullptr;
try {
skipdata = new char[skip];
} catch (const std::bad_alloc&) {
Expand Down
8 changes: 5 additions & 3 deletions modules/stream_manager/src/cpp/PrintfFunction.cpp
Expand Up @@ -13,10 +13,10 @@
//=============================================================================
#include <cstdio>
#include <vector>
#include <boost/algorithm/string.hpp>
#include "PrintfFunction.hpp"
#include "Error.hpp"
#include "PrintfHelper.hpp"
#include <boost/algorithm/string.hpp>
//=============================================================================
namespace Nelson {
//=============================================================================
Expand Down Expand Up @@ -134,7 +134,8 @@ printfFunction(const ArrayOfVector& args, std::wstring& errorMessage, std::wstri
boost::replace_all(f, L"d", L"lld");
nprn = swprintf(nbuff, BUFFER_SIZE_MAX, f.c_str(), data);
} else {
nprn = swprintf(nbuff, BUFFER_SIZE_MAX, dp, data);
std::wstring fmt = std::wstring(dp);
nprn = swprintf(nbuff, BUFFER_SIZE_MAX, fmt.c_str(), data);
}
nbuff[nprn + 1] = L'\0';
result = result + nbuff;
Expand All @@ -152,7 +153,8 @@ printfFunction(const ArrayOfVector& args, std::wstring& errorMessage, std::wstri
return false;
}
if (!isEmpty) {
nprn = swprintf(nbuff, BUFFER_SIZE_MAX, dp, data);
std::wstring fmt = std::wstring(dp);
nprn = swprintf(nbuff, BUFFER_SIZE_MAX, fmt.c_str(), data);
nbuff[nprn + 1] = L'\0';
result = result + nbuff;
}
Expand Down
7 changes: 3 additions & 4 deletions modules/stream_manager/src/fmt/include/fmt/core.h
Expand Up @@ -610,8 +610,7 @@ void check_format_string(S);

struct error_handler {
constexpr error_handler() = default;
constexpr error_handler(const error_handler&) = default; // lgtm [cpp/rule-of-two]

constexpr error_handler(const error_handler&) = default;
// This function is intentionally not constexpr to give a compile-time error.
FMT_NORETURN FMT_API void on_error(const char* message);
};
Expand Down Expand Up @@ -1381,13 +1380,13 @@ class appender : public std::back_insert_iterator<detail::buffer<char>> {
using _Unchecked_type = appender; // Mark iterator as checked.

auto operator++() -> appender& {
base::operator++(); // lgtm [cpp/useless-expression]
base::operator++();
return *this;
}

auto operator++(int) -> appender {
auto tmp = *this;
++*this; // lgtm [cpp/useless-expression]
++*this;
return tmp;
}
};
Expand Down
2 changes: 1 addition & 1 deletion modules/stream_manager/src/fmt/include/fmt/os.h
Expand Up @@ -367,7 +367,7 @@ FMT_BEGIN_DETAIL_NAMESPACE
struct buffer_size {
buffer_size() = default;
size_t value = 0;
buffer_size operator=(size_t val) const { // lgtm [cpp/assignment-does-not-return-this]
buffer_size operator=(size_t val) const {
auto bs = buffer_size();
bs.value = val;
return bs;
Expand Down

0 comments on commit 1058dbe

Please sign in to comment.