lib/utils: small cleanups

Fix some static analysis warnings
This commit is contained in:
Dirk-Jan C. Binnema
2026-04-10 00:12:50 +03:00
committed by Seth Ladygo
parent d8afabcc0f
commit 6715ff418a
5 changed files with 33 additions and 28 deletions

View File

@ -1,5 +1,5 @@
/*
** Copyright (C) 2020-2023 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
** Copyright (C) 2020-2026 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** This program is free software; you can redistribute it and/or modify it
** under the terms of the GNU General Public License as published by the
@ -36,8 +36,8 @@ Command::string_vec_arg(const std::string& name) const
std::vector<std::string> vec;
for (const auto& item : val->list()) {
if (!item.stringp()) {
// mu_warning("command: non-string in string-list for {}: {}",
// name, to_string());
mu_warning("command: non-string in string-list for {}: {}",
name, to_string());
return Nothing;
} else
vec.emplace_back(item.string());
@ -63,7 +63,6 @@ validate(const CommandHandler::CommandInfoMap& cmap,
//
// so, we're looking for the odd-numbered parameters.
const auto param_it = cmd.find_arg(argname);
const auto&& param_val = std::next(param_it);
// it's an error when a required parameter is missing.
if (param_it == cmd.cend()) {
if (arginfo.required)
@ -73,6 +72,13 @@ validate(const CommandHandler::CommandInfoMap& cmap,
continue; // not required
}
// the keyword is present; its value must follow it.
const auto param_val = std::next(param_it);
if (param_val == cmd.cend())
return Err(Error::Code::Command,
"missing value for parameter {} in command '{}'",
argname, cmd.to_string());
// the types must match, but the 'nil' symbol is acceptable as "no value"
if (param_val->type() != arginfo.type && !(param_val->nilp()))
return Err(Error::Code::Command,

View File

@ -112,7 +112,7 @@ public:
* @return true or false
*/
bool looking_at(std::string_view str) const {
if (pos_ >= html_.size() || pos_ + str.size() >= html_.size())
if (pos_ >= html_.size() || pos_ + str.size() > html_.size())
return false;
else
return matches({html_.data()+pos_, str.size()}, str);

View File

@ -89,9 +89,10 @@ Mu::canonicalize_filename(const std::string& path, const std::string& relative_t
path.c_str(),
relative_to.empty() ? nullptr : relative_to.c_str())).value()};
// remove trailing '/'... is this needed?
if (str[str.length()-1] == G_DIR_SEPARATOR)
str.erase(str.length() - 1);
if (!str.empty()) { // remove trailing '/'... is this needed?
if (str[str.length()-1] == G_DIR_SEPARATOR)
str.erase(str.length() - 1);
}
return str;
}
@ -175,7 +176,6 @@ Mu::read_from_stdin()
{
g_autoptr(GOutputStream) outmem = g_memory_output_stream_new_resizable();
g_autoptr(GInputStream) input = g_unix_input_stream_new(STDIN_FILENO, TRUE);
//g_autoptr(GCancellable) cancel{maybe_cancellable_timeout(timeout)};
GError *err{};
auto bytes = g_output_stream_splice(outmem, input,

View File

@ -552,9 +552,8 @@ Mu::parse_size(const std::string& val, bool is_first)
minfo = NULL;
if (g_regex_match(rx, val.c_str(), (GRegexMatchFlags)0, &minfo)) {
char* s;
s = g_match_info_fetch(minfo, 1);
size = atoll(s);
char* s = g_match_info_fetch(minfo, 1);
size = atoll(s); // check overflow?
g_free(s);
s = g_match_info_fetch(minfo, 2);
@ -676,14 +675,10 @@ Mu::summarize(const std::string& str, size_t max_lines)
static bool
locale_is_utf8 (void)
locale_is_utf8 ()
{
const gchar *dummy;
static int is_utf8 = -1;
if (G_UNLIKELY(is_utf8 == -1))
is_utf8 = g_get_charset(&dummy) ? 1 : 0;
return !!is_utf8;
static const bool is_utf8{g_get_charset({}) ? true : false};
return is_utf8;
}
bool

View File

@ -218,7 +218,7 @@ utf8_flatten(const std::string& s) {
*
* @return a cleaned-up string.
*/
std::string utf8_clean(const std::string& dirty);
[[nodiscard]] std::string utf8_clean(const std::string& dirty);
/**
@ -228,7 +228,7 @@ std::string utf8_clean(const std::string& dirty);
*
* @return string
*/
std::string utf8_wordbreak(const std::string& txt);
[[nodiscard]] std::string utf8_wordbreak(const std::string& txt);
/**
@ -239,7 +239,7 @@ std::string utf8_wordbreak(const std::string& txt);
*
* @return the string without control characters
*/
std::string remove_ctrl(const std::string& str);
[[nodiscard]] std::string remove_ctrl(const std::string& str);
/**
* Split a string in parts. As a special case, splitting an empty string
@ -250,7 +250,8 @@ std::string remove_ctrl(const std::string& str);
*
* @return the parts.
*/
std::vector<std::string> split(const std::string& str, const std::string& sepa);
[[nodiscard]] std::vector<std::string> split(const std::string& str,
const std::string& sepa);
/**
* Split a string in parts. As a special case, splitting an empty string
@ -271,8 +272,10 @@ std::vector<std::string> split(const std::string& str, char sepa);
*
* @return string
*/
std::string join(const std::vector<std::string>& svec, const std::string& sepa);
static inline std::string join(const std::vector<std::string>& svec, char sepa) {
[[nodiscard]] std::string join(const std::vector<std::string>& svec,
const std::string& sepa);
[[nodiscard]] static inline std::string join(const std::vector<std::string>& svec,
char sepa) {
return join(svec, std::string(1, sepa));
}
@ -314,7 +317,7 @@ static inline bool mu_print_encoded(fmt::format_string<T...> frm, T&&... args) n
*/
constexpr ::time_t time_t_min = 0;
constexpr ::time_t time_t_max = std::numeric_limits<::time_t>::max();
constexpr ::time_t to_time_t(int64_t t) {
[[nodiscard]] constexpr ::time_t to_time_t(int64_t t) {
return std::clamp(t,
static_cast<int64_t>(time_t_min),
static_cast<int64_t>(time_t_max));
@ -333,7 +336,8 @@ constexpr ::time_t to_time_t(int64_t t) {
*
* @return the corresponding time_t or Nothing if parsing failed.
*/
Option<::time_t> parse_date_time(const std::string& date, bool first, bool use_utc=false);
[[nodiscard]] Option<::time_t> parse_date_time(const std::string& date,
bool first, bool use_utc=false);
/**
* Crudely convert HTML to plain text. This attempts to scrape the
@ -343,7 +347,7 @@ Option<::time_t> parse_date_time(const std::string& date, bool first, bool use_u
*
* @return plain text
*/
std::string html_to_text(const std::string& html);
[[nodiscard]] std::string html_to_text(const std::string& html);
/**
* Hack to avoid locale crashes