microsoft / microsoft/Detours

Divide by zero in 'cping.cpp'

Open
#267 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
C++
Stars
6.4k
Forks
1.2k
PR merge metrics
No merged PRs in 30d

Description

Building and running the sample client bin.X64\cping.exe /i /l localhost, I got a divide by zero exception at this line:

s_rllCycles[E_DCOM] /= s_rllCounts[E_DCOM];

Not sure why. Some issue with incorrect CPU-speed calculation?
But I patched it like this:

--- a/samples/cping/cping.cpp 2022-12-24 09:26:38
+++ b/samples/cping/cping.cpp 2022-12-24 12:55:27
@@ -384,14 +384,19 @@
     DumpCycles(E_NET);
 #endif

-    s_rllCycles[E_DCOM] /= s_rllCounts[E_DCOM];
-    s_rllCycles[E_RPC] /= s_rllCounts[E_DCOM];
-    s_rllCycles[E_UDP] /= s_rllCounts[E_DCOM];
-    s_rllCycles[E_NET] /= s_rllCounts[E_DCOM];
-    s_rllTotals[E_DCOM] /= s_rllCounts[E_DCOM];
-    s_rllTotals[E_RPC] /= s_rllCounts[E_DCOM];
-    s_rllTotals[E_UDP] /= s_rllCounts[E_DCOM];
-    s_rllTotals[E_NET] /= s_rllCounts[E_DCOM];
+#define SAFE_DIV(res, div) do {             \
+                             if (div)       \
+                                res /= div; \
+                           } while (0)
+
+    SAFE_DIV (s_rllCycles[E_DCOM], s_rllCounts[E_DCOM]);
+    SAFE_DIV (s_rllCycles[E_RPC], s_rllCounts[E_DCOM]);
+    SAFE_DIV (s_rllCycles[E_UDP], s_rllCounts[E_DCOM]);
+    SAFE_DIV (s_rllCycles[E_NET], s_rllCounts[E_DCOM]);
+    SAFE_DIV (s_rllTotals[E_DCOM], s_rllCounts[E_DCOM]);
+    SAFE_DIV (s_rllTotals[E_RPC], s_rllCounts[E_DCOM]);
+    SAFE_DIV (s_rllTotals[E_UDP], s_rllCounts[E_DCOM]);
+    SAFE_DIV (s_rllTotals[E_NET], s_rllCounts[E_DCOM]);

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in samples/cping/cping.cpp at the division near line 387, then run bin.X64\cping.exe /i /l localhost to reproduce the exception. Compare the reported SAFE_DIV patch with the surrounding counter calculations and verify that the sample completes without a divide-by-zero exception when s_rllCounts[E_DCOM] is zero.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
devtools
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.