From beee22a95ed9044950e5486f22edd813e00ffa2c Mon Sep 17 00:00:00 2001 From: Kamikadze <40305144+Kam1k4dze@users.noreply.github.com> Date: Thu, 14 Aug 2025 19:44:56 +0500 Subject: refactor: Use new hyprutils casts (#11377) --- src/helpers/AsyncDialogBox.cpp | 4 ++-- src/helpers/Color.cpp | 2 +- src/helpers/Format.cpp | 2 +- src/helpers/MiscFunctions.cpp | 32 ++++++++++++++++---------------- src/helpers/Monitor.cpp | 19 ++++++++++--------- src/helpers/SdDaemon.cpp | 2 +- src/helpers/math/Math.cpp | 3 ++- 7 files changed, 33 insertions(+), 31 deletions(-) (limited to 'src/helpers') diff --git a/src/helpers/AsyncDialogBox.cpp b/src/helpers/AsyncDialogBox.cpp index 250b2f5a..6257dcb0 100644 --- a/src/helpers/AsyncDialogBox.cpp +++ b/src/helpers/AsyncDialogBox.cpp @@ -44,7 +44,7 @@ CAsyncDialogBox::CAsyncDialogBox(const std::string& title, const std::string& de } static int onFdWrite(int fd, uint32_t mask, void* data) { - auto box = (CAsyncDialogBox*)data; + auto box = sc(data); // lock the box to prevent a UAF auto lock = box->lockSelf(); @@ -68,7 +68,7 @@ void CAsyncDialogBox::onWrite(int fd, uint32_t mask) { } while ((ret = read(m_pipeReadFd.get(), buf.data(), 1023)) > 0) { - m_stdout += std::string_view{(char*)buf.data(), (size_t)ret}; + m_stdout += std::string_view{(buf.data()), sc(ret)}; } // restore the flags (otherwise libwayland won't give us a hangup) diff --git a/src/helpers/Color.cpp b/src/helpers/Color.cpp index 42ae1fd4..d8f3b02b 100644 --- a/src/helpers/Color.cpp +++ b/src/helpers/Color.cpp @@ -25,7 +25,7 @@ CHyprColor::CHyprColor(const Hyprgraphics::CColor& color, float a_) : a(a_) { } uint32_t CHyprColor::getAsHex() const { - return (uint32_t)(a * 255.f) * 0x1000000 + (uint32_t)(r * 255.f) * 0x10000 + (uint32_t)(g * 255.f) * 0x100 + (uint32_t)(b * 255.f) * 0x1; + return sc(a * 255.f) * 0x1000000 + sc(r * 255.f) * 0x10000 + sc(g * 255.f) * 0x100 + sc(b * 255.f) * 0x1; } Hyprgraphics::CColor::SSRGB CHyprColor::asRGB() const { diff --git a/src/helpers/Format.cpp b/src/helpers/Format.cpp index c3d72185..e03569a6 100644 --- a/src/helpers/Format.cpp +++ b/src/helpers/Format.cpp @@ -222,7 +222,7 @@ const SPixelFormat* NFormatUtils::getPixelFormatFromDRM(DRMFormat drm) { const SPixelFormat* NFormatUtils::getPixelFormatFromGL(uint32_t glFormat, uint32_t glType, bool alpha) { for (auto const& fmt : GLES3_FORMATS) { - if (fmt.glFormat == (int)glFormat && fmt.glType == (int)glType && fmt.withAlpha == alpha) + if (fmt.glFormat == sc(glFormat) && fmt.glType == sc(glType) && fmt.withAlpha == alpha) return &fmt; } diff --git a/src/helpers/MiscFunctions.cpp b/src/helpers/MiscFunctions.cpp index 57bb0f9b..2a1e0671 100644 --- a/src/helpers/MiscFunctions.cpp +++ b/src/helpers/MiscFunctions.cpp @@ -87,7 +87,7 @@ std::string escapeJSONStrings(const std::string& str) { case '\t': oss << "\\t"; break; default: if ('\x00' <= c && c <= '\x1f') { - oss << "\\u" << std::hex << std::setw(4) << std::setfill('0') << static_cast(c); + oss << "\\u" << std::hex << std::setw(4) << std::setfill('0') << sc(c); } else { oss << c; } @@ -213,7 +213,7 @@ SWorkspaceIDName getWorkspaceIDNameFromString(const std::string& in) { if (!PLUSMINUSRESULT.has_value()) return {WORKSPACE_INVALID}; - result.id = (int)PLUSMINUSRESULT.value(); + result.id = sc(PLUSMINUSRESULT.value()); WORKSPACEID remains = result.id; @@ -251,7 +251,7 @@ SWorkspaceIDName getWorkspaceIDNameFromString(const std::string& in) { remains -= 1; // traverse valid workspaces until we reach the remains - if ((size_t)remains < namedWSes.size()) { + if (sc(remains) < namedWSes.size()) { result.id = namedWSes[remains]; } else { remains -= namedWSes.size(); @@ -272,7 +272,7 @@ SWorkspaceIDName getWorkspaceIDNameFromString(const std::string& in) { char walkDir = in[1]; // sanitize. 0 means invalid oob in - - predictedWSID = std::max(predictedWSID, static_cast(0)); + predictedWSID = std::max(predictedWSID, sc(0)); // Count how many invalidWSes are in between (how bad the prediction was) WORKSPACEID beginID = in[1] == '+' ? activeWSID + 1 : predictedWSID; @@ -295,7 +295,7 @@ SWorkspaceIDName getWorkspaceIDNameFromString(const std::string& in) { } currentItem += remains; - currentItem = std::max(currentItem, static_cast(0)); + currentItem = std::max(currentItem, sc(0)); if (currentItem >= namedWSes.size()) { // At the seam between namedWSes and normal WSes. Behave like r+[diff] at imaginary ws 0 size_t diff = currentItem - (namedWSes.size() - 1); @@ -332,7 +332,7 @@ SWorkspaceIDName getWorkspaceIDNameFromString(const std::string& in) { // Need remainingWSes more auto namedWSIdx = namedWSes.size() - remainingWSes; // Sanitze - namedWSIdx = std::clamp(namedWSIdx, static_cast(0), namedWSes.size() - static_cast(1)); + namedWSIdx = std::clamp(namedWSIdx, sc(0), namedWSes.size() - sc(1)); finalWSID = namedWSes[namedWSIdx]; } else { // Couldn't find valid workspace in negative direction, search last first one back up positive direction @@ -376,10 +376,10 @@ SWorkspaceIDName getWorkspaceIDNameFromString(const std::string& in) { if (!PLUSMINUSRESULT.has_value()) return {WORKSPACE_INVALID}; - result.id = (int)PLUSMINUSRESULT.value(); + result.id = sc(PLUSMINUSRESULT.value()); // result now has +/- what we should move on mon - int remains = (int)result.id; + int remains = sc(result.id); std::vector validWSes; for (auto const& ws : g_pCompositor->getWorkspaces()) { @@ -400,7 +400,7 @@ SWorkspaceIDName getWorkspaceIDNameFromString(const std::string& in) { // clamp if (currentItem < 0) { currentItem = 0; - } else if (currentItem >= (ssize_t)validWSes.size()) { + } else if (currentItem >= sc(validWSes.size())) { currentItem = validWSes.size() - 1; } } else { @@ -409,7 +409,7 @@ SWorkspaceIDName getWorkspaceIDNameFromString(const std::string& in) { // get the current item WORKSPACEID activeWSID = g_pCompositor->m_lastMonitor->m_activeWorkspace ? g_pCompositor->m_lastMonitor->m_activeWorkspace->m_id : 1; - for (ssize_t i = 0; i < (ssize_t)validWSes.size(); i++) { + for (ssize_t i = 0; i < sc(validWSes.size()); i++) { if (validWSes[i] == activeWSID) { currentItem = i; break; @@ -420,7 +420,7 @@ SWorkspaceIDName getWorkspaceIDNameFromString(const std::string& in) { currentItem += remains; // sanitize - if (currentItem >= (ssize_t)validWSes.size()) { + if (currentItem >= sc(validWSes.size())) { currentItem = currentItem % validWSes.size(); } else if (currentItem < 0) { currentItem = validWSes.size() + currentItem; @@ -436,7 +436,7 @@ SWorkspaceIDName getWorkspaceIDNameFromString(const std::string& in) { if (!PLUSMINUSRESULT.has_value()) return {WORKSPACE_INVALID}; - result.id = std::max((int)PLUSMINUSRESULT.value(), 1); + result.id = std::max(sc(PLUSMINUSRESULT.value()), 1); } else { Debug::log(ERR, "Relative workspace on no mon!"); return {WORKSPACE_INVALID}; @@ -642,7 +642,7 @@ std::expected configStringToInt(const std::string& VALUE) a = std::round(std::stof(trim(rolling.substr(0, rolling.find(',')))) * 255.f); } catch (std::exception& e) { return std::unexpected("failed parsing " + VALUEWITHOUTFUNC); } - return a * (Hyprlang::INT)0x1000000 + *r * (Hyprlang::INT)0x10000 + *g * (Hyprlang::INT)0x100 + *b; + return a * sc(0x1000000) + *r * sc(0x10000) + *g * sc(0x100) + *b; } else if (VALUEWITHOUTFUNC.length() == 8) { const auto RGBA = parseHex(VALUEWITHOUTFUNC); @@ -670,7 +670,7 @@ std::expected configStringToInt(const std::string& VALUE) if (!r || !g || !b) return std::unexpected("failed parsing " + VALUEWITHOUTFUNC); - return (Hyprlang::INT)0xFF000000 + *r * (Hyprlang::INT)0x10000 + *g * (Hyprlang::INT)0x100 + *b; + return sc(0xFF000000) + *r * sc(0x10000) + *g * sc(0x100) + *b; } else if (VALUEWITHOUTFUNC.length() == 6) { auto r = parseHex(VALUEWITHOUTFUNC); return r ? *r + 0xFF000000 : r; @@ -717,7 +717,7 @@ Vector2D configStringToVector2D(const std::string& VALUE) { if (std::getline(iss, token)) throw std::invalid_argument("Invalid string format"); - return Vector2D((double)x, (double)y); + return Vector2D(sc(x), sc(y)); } double normalizeAngleRad(double ang) { @@ -910,7 +910,7 @@ std::expected binaryNameForPid(pid_t pid) { sysctl(mib, miblen, &exe, &sz, NULL, 0); std::string path = exe; #else - std::string path = std::format("/proc/{}/exe", (uint64_t)pid); + std::string path = std::format("/proc/{}/exe", sc(pid)); #endif std::error_code ec; diff --git a/src/helpers/Monitor.cpp b/src/helpers/Monitor.cpp index 561eab8c..37f650a1 100644 --- a/src/helpers/Monitor.cpp +++ b/src/helpers/Monitor.cpp @@ -213,7 +213,7 @@ void CMonitor::onConnect(bool noRule) { m_damage.setSize(m_transformedSize); - Debug::log(LOG, "Added new monitor with name {} at {:j0} with size {:j0}, pointer {:x}", m_output->name, m_position, m_pixelSize, (uintptr_t)m_output.get()); + Debug::log(LOG, "Added new monitor with name {} at {:j0} with size {:j0}, pointer {:x}", m_output->name, m_position, m_pixelSize, rc(m_output.get())); setupDefaultWS(monitorRule); @@ -577,7 +577,7 @@ bool CMonitor::applyMonitorRule(SMonitorRule* pMonitorRule, bool force) { addBest3Modes([](auto const& a, auto const& b) { if (std::round(a->refreshRate) > std::round(b->refreshRate)) return true; - else if (DELTALESSTHAN((float)a->refreshRate, (float)b->refreshRate, 1.F) && a->pixelSize.x > b->pixelSize.x && a->pixelSize.y > b->pixelSize.y) + else if (DELTALESSTHAN(sc(a->refreshRate), sc(b->refreshRate), 1.F) && a->pixelSize.x > b->pixelSize.x && a->pixelSize.y > b->pixelSize.y) return true; return false; }); @@ -770,7 +770,7 @@ bool CMonitor::applyMonitorRule(SMonitorRule* pMonitorRule, bool force) { bool set10bit = false; - for (auto const& fmt : formats[(int)!RULE->enable10bit]) { + for (auto const& fmt : formats[sc(!RULE->enable10bit)]) { m_output->state->setFormat(fmt.second); m_prevDrmFormat = m_drmFormat; m_drmFormat = fmt.second; @@ -902,8 +902,8 @@ bool CMonitor::applyMonitorRule(SMonitorRule* pMonitorRule, bool force) { // reload to fix mirrors g_pConfigManager->m_wantsMonitorReload = true; - Debug::log(LOG, "Monitor {} data dump: res {:X}@{:.2f}Hz, scale {:.2f}, transform {}, pos {:X}, 10b {}", m_name, m_pixelSize, m_refreshRate, m_scale, (int)m_transform, - m_position, (int)m_enabled10bit); + Debug::log(LOG, "Monitor {} data dump: res {:X}@{:.2f}Hz, scale {:.2f}, transform {}, pos {:X}, 10b {}", m_name, m_pixelSize, m_refreshRate, m_scale, sc(m_transform), + m_position, sc(m_enabled10bit)); EMIT_HOOK_EVENT("monitorLayoutChanged", nullptr); @@ -1003,7 +1003,7 @@ void CMonitor::setupDefaultWS(const SMonitorRule& monitorRule) { auto PNEWWORKSPACE = g_pCompositor->getWorkspaceByID(wsID); - Debug::log(LOG, "New monitor: WORKSPACEID {}, exists: {}", wsID, (int)(PNEWWORKSPACE != nullptr)); + Debug::log(LOG, "New monitor: WORKSPACEID {}, exists: {}", wsID, sc(PNEWWORKSPACE != nullptr)); if (PNEWWORKSPACE) { // workspace exists, move it to the newly connected monitor @@ -1078,7 +1078,7 @@ void CMonitor::setMirror(const std::string& mirrorOf) { setupDefaultWS(RULE); - applyMonitorRule((SMonitorRule*)&RULE, true); // will apply the offset and stuff + applyMonitorRule(const_cast(&RULE), true); // will apply the offset and stuff } else { PHLMONITOR BACKUPMON = nullptr; for (auto const& m : g_pCompositor->m_monitors) { @@ -1469,7 +1469,8 @@ bool CMonitor::attemptDirectScanout() { if (!params.success || !PSURFACE->m_current.texture->m_eglImage /* dmabuf */) return false; - Debug::log(TRACE, "attemptDirectScanout: surface {:x} passed, will attempt, buffer {}", (uintptr_t)PSURFACE.get(), (uintptr_t)PSURFACE->m_current.buffer.m_buffer.get()); + Debug::log(TRACE, "attemptDirectScanout: surface {:x} passed, will attempt, buffer {}", rc(PSURFACE.get()), + rc(PSURFACE->m_current.buffer.m_buffer.get())); auto PBUFFER = PSURFACE->m_current.buffer.m_buffer; @@ -1532,7 +1533,7 @@ bool CMonitor::attemptDirectScanout() { if (m_lastScanout.expired()) { m_lastScanout = PCANDIDATE; - Debug::log(LOG, "Entered a direct scanout to {:x}: \"{}\"", (uintptr_t)PCANDIDATE.get(), PCANDIDATE->m_title); + Debug::log(LOG, "Entered a direct scanout to {:x}: \"{}\"", rc(PCANDIDATE.get()), PCANDIDATE->m_title); } m_scanoutNeedsCursorUpdate = false; diff --git a/src/helpers/SdDaemon.cpp b/src/helpers/SdDaemon.cpp index 1cf15741..d914eecf 100644 --- a/src/helpers/SdDaemon.cpp +++ b/src/helpers/SdDaemon.cpp @@ -47,7 +47,7 @@ int NSystemd::sdNotify(int unsetEnvironment, const char* state) { if (unixAddr.sun_path[0] == '@') unixAddr.sun_path[0] = '\0'; - if (connect(fd, (const sockaddr*)&unixAddr, sizeof(struct sockaddr_un)) < 0) + if (connect(fd, rc(&unixAddr), sizeof(struct sockaddr_un)) < 0) return -errno; // arbitrary value which seems to be enough for s-d messages diff --git a/src/helpers/math/Math.cpp b/src/helpers/math/Math.cpp index d111690e..f927701c 100644 --- a/src/helpers/math/Math.cpp +++ b/src/helpers/math/Math.cpp @@ -1,4 +1,5 @@ #include "Math.hpp" +#include "../memory/Memory.hpp" Hyprutils::Math::eTransform wlTransformToHyprutils(wl_output_transform t) { switch (t) { @@ -17,7 +18,7 @@ Hyprutils::Math::eTransform wlTransformToHyprutils(wl_output_transform t) { wl_output_transform invertTransform(wl_output_transform tr) { if ((tr & WL_OUTPUT_TRANSFORM_90) && !(tr & WL_OUTPUT_TRANSFORM_FLIPPED)) - tr = (wl_output_transform)(tr ^ (int)WL_OUTPUT_TRANSFORM_180); + tr = sc(tr ^ sc(WL_OUTPUT_TRANSFORM_180)); return tr; } -- cgit v1.2.3