for those of us tasked with making sure our code is secure we're always looking for ways that rogue agents can outsmart us. a while back i came across an article describing a timing attack by which adversaries would probe the system with inputs of various lengths to learn about system internals. it got me thinking and this is what i came up with. this routine will compare two equal length buffers in the same time whether they are identical or not. what do you think? is this helpful? would you care to see more coding tricks and tips in the future?
Boris Kogan’s Post
More Relevant Posts
-
day 4 -coding Challenge problem- #leetcode 1318- Minimum Flips to Make a OR b Equal to c. Given 3 positives numbers a, b and c. Return the minimum flips required in some bits of a and b to make ( a OR b == c ). (bitwise OR operation). Flip operation consists of change any single bit 1 to 0 or change the bit 0 to 1 in their binary representation. Input: a = 2, b = 6, c = 5 Output: 3 Explanation: After flips a = 1 , b = 4 , c = 5 such that (a OR b == c) Example 2: Input: a = 4, b = 2, c = 7 Output: 1 Example 3: Input: a = 1, b = 2, c = 3 Output: 0
To view or add a comment, sign in
-
-
🚀 Solved LeetCode Problem: Single Number (C++) 🔢 Every element appears twice except one This problem is a great reminder that bit manipulation can often beat complex data structures with pure logic and simplicity. 💡 Approach: Used the XOR operator (^) — since x ^ x = 0 and x ^ 0 = x, all duplicate numbers cancel out, leaving the single unique number. 🧠 Key Takeaways: Time Complexity: O(n) Space Complexity: O(1) Concept: Bit Manipulation + XOR Property 💬 Elegant, fast, and minimal — that’s the beauty of C++. 🔖 #LeetCode #Cplusplus #DSA #Coding #ProblemSolving #BitManipulation #CodingInterview #SoftwareEngineering #CodeNewbie #TechLearning #C++Developer #Algorithm #DataStructures #CodingChallenge #XOR #CodeDaily #LearnToCode
To view or add a comment, sign in
-
-
✨✨ Day #47 of #100DayOfDP 🔥 Today's Problem is LeetCode 583: Delete Operation for Two Strings problem. At first, I made a classic mistake — I implemented the Longest Common Substring instead of the Longest Common Subsequence (LCS). The difference? > Substring resets to 0 when characters don’t match. > Subsequence carries forward the max from previous states. That tiny change (dp[i][j] = max(dp[i-1][j], dp[i][j-1])) fixed everything. Final formula: 🔹 Deletions needed = (len(word1) – LCS) + (len(word2) – LCS) Example: word1 = "sea", word2 = "eat" LCS = "ea" → length = 2 Deletions = (3-2) + (3-2) = 2 ✅ This bug reminded me how easily we can confuse related problems, but also how powerful a small correction can be. 💡 Takeaway: Pay close attention to whether the problem needs subsequence or substring — one small difference changes the entire DP transition! #DynamicProgramming #Coding #LeetCode #ProblemSolving #SoftwareEngineering
To view or add a comment, sign in
-
-
Balancing Logic and Efficiency in C++ I recently explored a classic problem — finding the equilibrium index in an array, where the sum of elements on the left equals the sum on the right. At first, I used a brute-force approach with nested loops (O(n²)), but then optimized it to O(n) by using running sums — no extra space, no redundant computation. ✨ Key takeaway: Small changes in logic can lead to big improvements in performance. Efficient code isn’t just about solving problems — it’s about solving them smarter. #CPlusPlus #Coding #ProblemSolving #Optimization #SoftwareDevelopment #Efficiency
To view or add a comment, sign in
-
-
Optimization - A Bliss, A Curse Every C++ programmer learns this the hard way: the moment you start optimizing, you stop sleeping peacefully. At first, optimization feels like power - the thrill of shaving off microseconds, removing an allocation. But soon, it turns into a paradox, and you realize performance is rarely free. The Bliss: C++ gives you surgical control. Move semantics, inlining, constexpr evaluation, cache-friendly layouts. You can make code so fast it almost disappears from the profiler. You can feel the machine breathe with your design. You can measure a microsecond and know exactly why it exists. The Curse: Every optimization narrows your world. It ties your code to hardware behavior, compiler quirks, and a future version of yourself that must remember why you wrote it that way. Optimized code often fights readability, flexibility, and sometimes - correctness. The fastest path isn’t always the safest one. Premature optimization ? - That is a trap for your future self. The Balance: The art of optimization is not to make code fast. It is to make code right first, then fast enough, and no faster than necessary. Because optimization done well is invisible - it makes code efficient without stealing its clarity. Done poorly, it turns every line into a puzzle. A Take ? Optimization in C++ is both the weapon and the wound. Use it deliberately, not desperately. And remember: the most optimized code is the one you don’t have to debug at 2 a.m. When was the last time an optimization saved you - or came back to haunt you? #ThreadThursday #Cplusplus #ModernCpp #Performance #Optimization #SoftwareEngineering #CleanCode #ProgrammingPhilosophy
To view or add a comment, sign in
-
🟥 1. C++ – Bad Example Demonstrates unsafe code that doesn’t verify if the player is alive before performing actions. Relies on basic structs without proper type safety. Can lead to runtime errors or undefined behavior. Highlights a common issue in older or less-structured C++ codebases. 🟩 2. Rust – Good Example Uses enums and pattern matching to represent safe, valid states. The compiler enforces handling of all possible cases (e.g., Alive, Dead). Prevents logical errors like calling methods on invalid objects. Rust’s strong type system eliminates many runtime bugs early. 🟩 3. C++ – Good Example (Using std::variant) Modern C++ can achieve Rust-like safety using std::variant and std::visit. Each player state (AlivePlayer, DeadPlayer) is modeled distinctly. The compiler ensures that all possible variants are handled safely. Demonstrates type-safe polymorphism without traditional inheritance. 💡 Summary Insight: Rust enforces safety at compile time by design, while modern C++ provides similar capabilities through advanced features like std::variant. This comparison shows how explicit state modeling leads to safer and more maintainable code.
To view or add a comment, sign in
-
-
#100DaysOfLeetCode – Day 62 🚀 🔹 3186 - Maximum Total Damage With Spell Casting -> The "No Neighbors, No Next-Door-Neighbors" Rule: If I use spell D, I must skip D±1 and D±2. -> This constraint breaks the problem into independent, non-conflicting groups of spells separated by at least 3. -> Solution: Dynamic Programming on the sorted, distinct power levels to decide the maximum total damage for each group. The core choice is simple: cast the current spell, or skip it. 💡 Takeaway: When seemingly complex constraints exist, look for how they logically disrupt the input sequence. That disruption often defines the non-overlapping subproblems for your DP solution. #Day62 #100DaysOfLeetCode #DP #Constraints #Optimization
To view or add a comment, sign in
-
-
💡 𝐂#/.𝐍𝐄𝐓 𝐄𝐱𝐜𝐞𝐩𝐭𝐢𝐨𝐧 𝐇𝐚𝐧𝐝𝐥𝐢𝐧𝐠 𝐓𝐢𝐩 🔥 💎𝐃𝐨𝐧'𝐭 𝐮𝐬𝐞 𝐭𝐡𝐫𝐨𝐰 𝐞𝐱 𝐢𝐧 𝐜𝐚𝐭𝐜𝐡 𝐛𝐥𝐨𝐠 🚫 𝐭𝐡𝐫𝐨𝐰 𝐞𝐱; updates the StackTrace property of ex. If you throw the "ex", 𝐭𝐡𝐞 𝐬𝐭𝐚𝐜𝐤 𝐭𝐫𝐚𝐜𝐞 𝐢𝐬 𝐥𝐨𝐬𝐭! ✅ 𝐭𝐡𝐫𝐨𝐰; preserves the original stack trace of exception stored in the 𝐄𝐱𝐜𝐞𝐩𝐭𝐢𝐨𝐧.𝐒𝐭𝐚𝐜𝐤𝐓𝐫𝐚𝐜𝐞 property. ⚡ In this way, 𝐰𝐞 𝐝𝐨𝐧𝐭 𝐥𝐨𝐬𝐞 𝐭𝐡𝐞 𝐬𝐭𝐚𝐜𝐤 𝐭𝐫𝐚𝐜𝐞 and can quickly find the root cause of the error. ⚠️ 𝐂𝐀𝟐𝟐𝟎𝟎 𝐖𝐚𝐫𝐧𝐢𝐧𝐠 • CA2200 is a code analysis rule that detects improper exception re-throwing. • It triggers when you use throw ex; instead of throw; in catch blocks. • Following this rule helps maintain complete error information for troubleshooting. ⚠️ 𝐭𝐡𝐫𝐨𝐰 𝐞𝐱, 𝐰𝐢𝐥𝐥 𝐫𝐞𝐬𝐞𝐭 𝐭𝐡𝐞 𝐜𝐚𝐥𝐥 𝐬𝐭𝐚𝐜𝐤 𝐢𝐧 𝐭𝐡𝐞 𝐞𝐱𝐜𝐞𝐩𝐭𝐢𝐨𝐧 𝐭𝐨 𝐭𝐡𝐞 𝐩𝐨𝐢𝐧𝐭 𝐰𝐡𝐞𝐫𝐞 𝐭𝐡𝐢𝐬 𝐭𝐡𝐫𝐨𝐰 𝐬𝐭𝐚𝐭𝐞𝐦𝐞𝐧𝐭 𝐥𝐨𝐬𝐞𝐬 𝐭𝐡𝐞 𝐢𝐧𝐟𝐨𝐫𝐦𝐚𝐭𝐢𝐨𝐧 𝐚𝐛𝐨𝐮𝐭 𝐰𝐡𝐞𝐫𝐞 𝐭𝐡𝐞 𝐞𝐱𝐜𝐞𝐩𝐭𝐢𝐨𝐧 𝐰𝐚𝐬 𝐜𝐫𝐞𝐚𝐭𝐞𝐝. So, we can't see the original stack trace and you may spend days to find the root cause. #csharp #dotnet #programming #softwareengineering #softwaredevelopment
To view or add a comment, sign in
-
-
🚀 Day 98 of My #LeetCode Journey: Today’s problem: “Adjacent Increasing Subarrays Detection I” (LeetCode #3349) 🧩 The task was to determine if there exist two adjacent subarrays of length k that are both strictly increasing — and surprisingly, it required a mix of clean logic and efficient iteration. 💡 Key Insight: By tracking increasing sequences and marking valid subarrays, we can efficiently detect adjacent strictly increasing pairs in just O(n) time. 🧠 Concepts Used: Sliding window logic Incremental counting of strictly increasing runs Boolean marking of valid subarrays 🔥 Takeaway: Sometimes, elegant logic beats brute force — clean thinking leads to clean code. #LeetCode #CodingChallenge #CPlusPlus #ProblemSolving #DailyPractice #CompetitiveProgramming #100DaysOfCode
To view or add a comment, sign in
-
-
Many people think a programmer is a Hollywood-style hacker 🎬 — a few clicks and you’re in the CIA database. Reality? Hours of debugging, endless testing, and tiny fixes that take forever. Debugging is a marathon, not a sprint. 🧩 And that’s the real magic of coding. 🚀
To view or add a comment, sign in
More from this author
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development
Thanks, interesting trick. Its the only problem is that this code could theoretically give false positive due to uint32 overflow. You could replace it with uint64 and reduce the chance of the problem. I'm wondering, if it is possible to implement this code without even theoretical possibility of false positive? Will think about it on my way home... And yes, I'd be curious to see more tricks like this. Or unlike this. Both.