From: Egor Date: Sat, 31 Jan 2026 21:02:53 +0000 (+0200) Subject: job: use std::move_only_function X-Git-Url: https://git.patchysicp.xyz/?a=commitdiff_plain;h=de89b32c033fdd89dfad89dc81f4131a64d3136f;p=patchyircd.git job: use std::move_only_function --- diff --git a/src/job.cpp b/src/job.cpp index f8730b6..da040a1 100644 --- a/src/job.cpp +++ b/src/job.cpp @@ -93,7 +93,7 @@ void job_kill(Client *cl) // FIXME: make a better version of this and use it throughout the codebase struct SecureStringWrapper { char *data; - operator char*() { return data; } + operator char*() const { return data; } SecureStringWrapper(const char *s) :data(nullptr) { if (s) { size_t len = strlen(s); @@ -102,12 +102,10 @@ struct SecureStringWrapper { data[len] = '\0'; } } - SecureStringWrapper(const SecureStringWrapper &) = delete; - SecureStringWrapper& operator=(const SecureStringWrapper &) = delete; - SecureStringWrapper(SecureStringWrapper &&other) { + SecureStringWrapper(SecureStringWrapper&& other) noexcept { data = std::exchange(other.data, nullptr); } - SecureStringWrapper& operator=(SecureStringWrapper &&other) { + SecureStringWrapper& operator=(SecureStringWrapper&& other) noexcept { std::swap(data, other.data); return *this; } @@ -120,13 +118,11 @@ struct SecureStringWrapper { }; static void job_password_check_internal(Client *client, const char *salt, const char *pw, contproc_t&& then) { - // FIXME: make_shared only really needed here cuz std::function wants a copyable object - // C++23 adds a std::move_only_function - job_add(client, [pw=std::make_shared(pw), salt=std::make_shared(salt)]() -> int { - if (!*salt) + job_add(client, [pw=SecureStringWrapper(pw), salt=SecureStringWrapper(salt)]() -> int { + if (!salt) return 1; - char *hash = password_crypt(*pw, *salt); - int rv = !hash || strcmp(hash, *salt) != 0; + char *hash = password_crypt(pw, salt); + int rv = !hash || strcmp(hash, salt) != 0; if (hash) memset(hash, 0, strlen(hash)); return rv; diff --git a/src/job.hpp b/src/job.hpp index 1d27887..a0ca166 100644 --- a/src/job.hpp +++ b/src/job.hpp @@ -3,8 +3,8 @@ #ifndef JOB_H_ #define JOB_H_ #include "ircdata.hpp" -using jobproc_t = std::function; -using contproc_t = std::function; +using jobproc_t = std::move_only_function; +using contproc_t = std::move_only_function; void job_init(void); void job_fini(void); void job_process(void);