[En-Nut-Discussion] NutSemTryWait Bug

ennut at thezachariasgroup.com ennut at thezachariasgroup.com
Mon Jun 16 16:23:37 CEST 2008


  I think I found a problem in NutSemTryWait in NUT\OS\semaphore.c

Original Code:

     int NutSemTryWait(SEM * sem) {
         if (sem->value < 0)
             return -1;
         else
             NutSemWait(sem);
         return 0;
     }

Suggested code:


     int NutSemTryWait(SEM * sem) {
         if (sem->value <= 0)
             return -1;
         else
             NutSemWait(sem);
         return 0;
     }

   If the sem->value was initialized to 1, and a routine has grabbed 
it, sem->value would be zero.
NutSemTryWait should return a -1 under these conditions since the sem 
is not available.
However sem->value is not less than zero and the code calls 
NutSemWait and waits for the sem to become free.
This is not the intent of the routine as I understand it.

   Thoughts?




More information about the En-Nut-Discussion mailing list