Skip to content

Commit

Permalink
Use snprintf instead of printf
Browse files Browse the repository at this point in the history
Also avoid raw new/deletes and use a std::string instead to hold the
result of the formatting operation.

Signed-off-by: Rodrigo Tobar <[email protected]>
  • Loading branch information
rtobar committed Jul 10, 2024
1 parent 5f89d3e commit 1f7a706
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/apps/fits_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,15 @@ Image from_fits(const std::string &filename, PixelScale &pixel_scale)
static
void write_header(std::ofstream &f, const char *header) {
char hdr[81];
std::sprintf(hdr, "%-80s", header);
std::snprintf(hdr, 81, "%-80s", header);
f.write(hdr, 80);
};

template <typename ...Ts>
static
void write_header(std::ofstream &f, const char *fmt, Ts&&...values) {
char hdr[81];
std::sprintf(hdr, fmt, std::forward<Ts>(values)...);
std::snprintf(hdr, 81, fmt, std::forward<Ts>(values)...);
write_header(f, hdr);
};

Expand Down
8 changes: 4 additions & 4 deletions src/apps/profit-cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ void show_version(std::ostream &os) {
os << endl;
}

static const char *help_msg = R"===(
static const char *help_msg_fmt = R"===(
%s: utility program to generate an image out of a model and a set of profiles
This program is licensed under the GPLv3 license.
Expand Down Expand Up @@ -247,10 +247,10 @@ For more information visit https://libprofit.readthedocs.io.
template <typename T>
static
void usage(std::basic_ostream<T> &os, char *prog_name) {
char *buff = new char[std::strlen(help_msg) - 4 + std::strlen(prog_name) * 2 + 1];
std::sprintf(buff, help_msg, prog_name, prog_name);
std::size_t bufsize = std::strlen(help_msg_fmt) - 4 + std::strlen(prog_name) * 2;
std::string buff(bufsize, 0);
std::snprintf(&buff[0], bufsize, help_msg_fmt, prog_name, prog_name);
os << buff;
delete []buff;
}

static
Expand Down

0 comments on commit 1f7a706

Please sign in to comment.