Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eliminate Sprintf warnings #3346

Merged
merged 3 commits into from
Mar 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/modlunit/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ int main(int argc, char* argv[]) {
static void openfiles(int argc, char* argv[]) {
char *cp, modprefix[NRN_BUFSIZE - 5];
if (argc > 1) {
assert(strlen(argv[1]) < NRN_BUFSIZE);
assert(strlen(argv[1]) < NRN_BUFSIZE - 5);
Sprintf(modprefix, "%s", argv[1]);
cp = strstr(modprefix, ".mod");
if (cp) {
Expand Down
1 change: 1 addition & 0 deletions src/modlunit/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ extern Item* qlint;
#define IGNORE(arg) arg
#endif
using neuron::Sprintf;
using neuron::SprintfAsrt;

/* model.h,v
* Revision 1.2 1997/11/24 16:19:13 hines
Expand Down
14 changes: 7 additions & 7 deletions src/nocmodl/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,15 +216,15 @@
char* current_line() { /* assumes we actually want the previous line */
static char buf[NRN_BUFSIZE];
char* p;
Sprintf(buf,
"at line %d in file %s:\\n%s",
linenum - 1,
SprintfAsrt(buf,
"at line %d in file %s:\\n%s",
linenum - 1,

Check warning on line 221 in src/nocmodl/io.cpp

View check run for this annotation

Codecov / codecov/patch

src/nocmodl/io.cpp#L221

Added line #L221 was not covered by tests
#if !defined(NRN_AVOID_ABSOLUTE_PATHS)
finname,
finname,
#else
fs::absolute(finname).filename().c_str(),
fs::absolute(finname).filename().c_str(),
#endif
inlinebuf[whichbuf ? 0 : 1] + 30);
inlinebuf[whichbuf ? 0 : 1] + 30);
for (p = buf; *p; ++p) {
if (*p == '\n') {
*p = '\0';
Expand Down Expand Up @@ -432,7 +432,7 @@
}

static void pop_file_stack() {
Sprintf(buf, ":::end INCLUDE %s\n", finname);
SprintfAsrt(buf, ":::end INCLUDE %s\n", finname);
lappendstr(filetxtlist, buf);
FileStackItem* fsi;
fsi = (FileStackItem*) (SYM(filestack->prev));
Expand Down
22 changes: 11 additions & 11 deletions src/nocmodl/kinetic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -987,7 +987,7 @@
Sprintf(eqstr, "%d(%d", fn, eqnum);
eqnum++;
}
Sprintf(buf, "_RHS%s) = %s;\n", eqstr, r->krate[0]);
SprintfAsrt(buf, "_RHS%s) = %s;\n", eqstr, r->krate[0]);
Insertstr(q, buf);
for (rt = r->rterm[0]; rt; rt = rt->rnext) {
char buf1[NRN_BUFSIZE];
Expand All @@ -1004,19 +1004,19 @@
Sprintf(buf, "_i = %s;\n", rt->str);
Insertstr(q, buf);
}
Sprintf(buf,
"_MATELM%s, %d + %s) = %d%s;\n",
eqstr,
rt->sym->varnum,
rt->str,
rt->num,
buf1);
SprintfAsrt(buf,

Check warning on line 1007 in src/nocmodl/kinetic.cpp

View check run for this annotation

Codecov / codecov/patch

src/nocmodl/kinetic.cpp#L1007

Added line #L1007 was not covered by tests
"_MATELM%s, %d + %s) = %d%s;\n",
eqstr,
rt->sym->varnum,
rt->str,
rt->num,

Check warning on line 1012 in src/nocmodl/kinetic.cpp

View check run for this annotation

Codecov / codecov/patch

src/nocmodl/kinetic.cpp#L1010-L1012

Added lines #L1010 - L1012 were not covered by tests
buf1);
Insertstr(q, buf);
Sprintf(buf, "_RHS%s) -= %s[%s]%s", eqstr, rt->sym->name, rt->str, buf1);
SprintfAsrt(buf, "_RHS%s) -= %s[%s]%s", eqstr, rt->sym->name, rt->str, buf1);

Check warning on line 1015 in src/nocmodl/kinetic.cpp

View check run for this annotation

Codecov / codecov/patch

src/nocmodl/kinetic.cpp#L1015

Added line #L1015 was not covered by tests
} else {
Sprintf(buf, "_MATELM%s, %d) = %d%s;\n", eqstr, rt->sym->varnum, rt->num, buf1);
SprintfAsrt(buf, "_MATELM%s, %d) = %d%s;\n", eqstr, rt->sym->varnum, rt->num, buf1);
Insertstr(q, buf);
Sprintf(buf, "_RHS%s) -= %s%s", eqstr, rt->sym->name, buf1);
SprintfAsrt(buf, "_RHS%s) -= %s%s", eqstr, rt->sym->name, buf1);
}
Insertstr(q, buf);
if (rt->num != 1) {
Expand Down
1 change: 1 addition & 0 deletions src/nocmodl/modl.h
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ extern Item* qlint;
#define Free(arg) free((void*) (arg))
#endif
using neuron::Sprintf;
using neuron::SprintfAsrt;

void verbatim_adjust(char* q);
/** @} */ // end of hoc_functions
10 changes: 5 additions & 5 deletions src/nocmodl/nocpout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1775,7 +1775,7 @@ void units_reg() {
if (s->nrntype & NRNGLOBAL) {
decode_ustr(s, &d1, &d2, u);
if (u[0]) {
Sprintf(buf, "{\"%s%s\", \"%s\"},\n", s->name, suffix, u);
SprintfAsrt(buf, "{\"%s%s\", \"%s\"},\n", s->name, suffix, u);
lappendstr(defs_list, buf);
}
}
Expand All @@ -1784,31 +1784,31 @@ void units_reg() {
s = SYM(q);
decode_ustr(s, &d1, &d2, u);
if (u[0]) {
Sprintf(buf, "{\"%s%s\", \"%s\"},\n", s->name, rsuffix, u);
SprintfAsrt(buf, "{\"%s%s\", \"%s\"},\n", s->name, rsuffix, u);
lappendstr(defs_list, buf);
}
}
ITERATE(q, rangestate) {
s = SYM(q);
decode_ustr(s, &d1, &d2, u);
if (u[0]) {
Sprintf(buf, "{\"%s%s\", \"%s\"},\n", s->name, rsuffix, u);
SprintfAsrt(buf, "{\"%s%s\", \"%s\"},\n", s->name, rsuffix, u);
lappendstr(defs_list, buf);
}
}
ITERATE(q, rangedep) {
s = SYM(q);
decode_ustr(s, &d1, &d2, u);
if (u[0]) {
Sprintf(buf, "{\"%s%s\", \"%s\"},\n", s->name, rsuffix, u);
SprintfAsrt(buf, "{\"%s%s\", \"%s\"},\n", s->name, rsuffix, u);
lappendstr(defs_list, buf);
}
}
ITERATE(q, nrnpointers) {
s = SYM(q);
decode_ustr(s, &d1, &d2, u);
if (u[0]) {
Sprintf(buf, "{\"%s%s\", \"%s\"},\n", s->name, rsuffix, u);
SprintfAsrt(buf, "{\"%s%s\", \"%s\"},\n", s->name, rsuffix, u);
lappendstr(defs_list, buf);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/oc/hocdec.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ int ilint;
#define Strncpy strncpy
#endif
using neuron::Sprintf;
using neuron::SprintfAsrt;

// No longer used because of clang format difficulty
// #define IFGUI if (hoc_usegui) {
Expand Down
2 changes: 1 addition & 1 deletion src/oc/mk_hocusr_h.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def processvar(a, names):

def remove_multiline_comments(string):
# remove all occurance comments (/*COMMENT */) from string
return re.sub(r"/\*.*?\*/", "", string, 0, re.DOTALL)
return re.sub(r"/\*.*?\*/", "", string, count=0, flags=re.DOTALL)


types = {}
Expand Down
18 changes: 18 additions & 0 deletions src/oc/wrap_sprintf.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include <cstdio>
#include <utility> // std::forward
#include <stdexcept>
#include <assert.h>

namespace neuron {
/**
Expand All @@ -17,4 +19,20 @@ int Sprintf(char (&buf)[N], const char* fmt, Args&&... args) {
return std::snprintf(buf, N, fmt, std::forward<Args>(args)...);
}
}

/**
* @brief assert if the Sprintf format data does not fit into buf
*/
template <std::size_t N, typename... Args>
void SprintfAsrt(char (&buf)[N], const char* fmt, Args&&... args) {
int sz = Sprintf(buf, fmt, std::forward<Args>(args)...);
#ifdef UNIT_TESTING
if (sz < 0 || std::size_t(sz) >= N) {
throw std::runtime_error("SprintfAsrt buffer too small or snprintf error");
}
#else
assert(sz >= 0 && std::size_t(sz) < N);
#endif
}

} // namespace neuron
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ add_executable(
unit_tests/container/node.cpp
unit_tests/node_order_optim/permutations.cpp
unit_tests/utils/enumerate.cpp
unit_tests/utils/Sprintf.cpp
unit_tests/oc/hoc_interpreter.cpp
cover/unit_tests/cover.cpp)
set(catch2_targets testneuron)
Expand Down
19 changes: 19 additions & 0 deletions test/unit_tests/utils/Sprintf.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <catch2/catch_test_macros.hpp>

#define UNIT_TESTING
#include "oc/wrap_sprintf.h"

using neuron::SprintfAsrt;

TEST_CASE("buf fits", "[NEURON]") {
char buf[20];
SprintfAsrt(buf, "%s", "hello");
REQUIRE(strcmp(buf, "hello") == 0);
}

TEST_CASE("buf too small", "[NEURON]") {
char buf[5];
char* s = strdup("hello");
REQUIRE_THROWS(SprintfAsrt(buf, "%s", s));
free(s);
}
Loading