[ofw] Division by zero in etc/user/gtod.c

Schmitt, Hubert Hubert.Schmitt at oce.com
Mon Dec 17 06:19:12 PST 2012


Hello everybody,
 
There is a potential bug (division by zero) in the gettimeofday()
function which should be fixed with suggested patch below.
The point is to move the 'QueryCounter = 1' behind calling
QueryPerformanceFrequency().
Otherwise another thread (within current process) could pre-empt the
current one right after 'QueryCounter = 1',
and thus would work with an uninitialized (i.e. zero) Frequency
variable.
 
Thanks,
Hubert.
 
 
Index: gtod.c
===================================================================
--- gtod.c (revision 3414)
+++ gtod.c (working copy)
@@ -62,6 +62,9 @@
 int gettimeofday(struct timeval *ptv, void *ignored)
 {
  static int QueryCounter = 2;
+ static LARGE_INTEGER Frequency = {10000000,0}; /* prevent division by
0 */
+ static LARGE_INTEGER Offset; /* counter offset for right time*/
+ static LARGE_INTEGER LastCounter;
  FILETIME CurrentTime;
  UNREFERENCED_PARAMETER(ignored);     
 
@@ -69,9 +72,6 @@
 
  if(QueryCounter)
  {
-  static LARGE_INTEGER Frequency;
-  static LARGE_INTEGER Offset; /* counter offset for right time*/
-  static LARGE_INTEGER LastCounter;
   LARGE_INTEGER Time;
   LARGE_INTEGER Counter;
  
@@ -80,13 +80,15 @@
  
   if(QueryCounter == 2)
   {
-   QueryCounter = 1;
-   if(!QueryPerformanceFrequency(&Frequency))
+   if(QueryPerformanceFrequency(&Frequency))
    {
+    QueryCounter = 1; // But now the Frequency is valid!
+   }
+   else
+   {
     QueryCounter = 0;
     Frequency.QuadPart = 10000000; /* prevent division by 0 */
    }
- 
    /* get time as a large integer */
    Counter.HighPart &= 0x7FL; /* Clear high bits to prevent overflow */
    Offset.LowPart = CurrentTime.dwLowDateTime;



This message and attachment(s) are intended solely for use by the addressee and may contain information that is privileged, confidential or otherwise exempt from disclosure under applicable law.

If you are not the intended recipient or agent thereof responsible for delivering this message to the intended recipient, you are hereby notified that any dissemination, distribution or copying of this communication is strictly prohibited.

If you have received this communication in error, please notify the sender immediately by telephone and with a 'reply' message.

Thank you for your co-operation.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openfabrics.org/pipermail/ofw/attachments/20121217/d68fe122/attachment.html>


More information about the ofw mailing list