int safe_vsprintf(std::string& buf, const char * fmt, va_list argptr) { char buffer[1024]; int ret = ::vsnprintf(buffer, sizeof(buffer), fmt, argptr); if (ret >= (int) sizeof(buffer)) { if (ret >= (int) buf.max_size()) { buf.clear(); printf("safe_vsprintf: string max_size limit(%d)", buf.max_size()); return 0; } buf.resize(ret); ret = vsnprintf(&buf.at(0), ret + 1, fmt, argptr); } else { buf = buffer; } //va_end(argptr); return ret; }
inline const std::string& path_to_self() { static constexpr const char self[] = "/proc/self/exe"; static std::string r; static std::once_flag f; std::call_once(f, []() { using N = decltype(readlink(self, &r.front(), r.size())); constexpr decltype(r.size()) max_path_sz{PATH_MAX}; N read_cnt; do { r.resize(std::max(2 * r.size(), max_path_sz)); read_cnt = readlink(self, &r.front(), r.size()); } while (read_cnt == -1 && r.size() < r.max_size()); r.resize(std::max(read_cnt, N{0})); }); return r; }