Menu

division by 0

michele
2015-11-09
2015-11-18
  • michele

    michele - 2015-11-09

    Hello,
    during my work I found that cppcheck doesn't detect me a possible fault: in this part of my code there is a division without any check done before in case the variable is null .. is there a way to force in cppcheck a verification of this condition?
    thanks
    michele

    ..
    int zBlockSize;
    int *pzNumBlocks;

    zBlockSize = HalDaf_GetSectorSize();

    / evaluate number of sectors /
    if (pzNumBlocks != NULL)
    {
    *pzNumBlocks =( ( MemSize ) / zBlockSize );
    }
    ..

     
  • michele

    michele - 2015-11-12

    I checked and I found that if I do like that cppcheck detects the division by 0. In my case the problem is more that the return of the function HalDaf_GetSectorSize() is unknown so it could be 0 .. and that check is not done. Is there a way to enable a check for that?
    Thanks for the support
    Michele

      U16 zDiv = 0u;
      U16 zTest = 16u;
    
      zTest = zTest/zDiv;
    
     
  • Daniel Marjamäki

    Thanks! This is a very good forum topic imho.

    Is there a way to enable a check for that?

    I don't think it's a good idea to add a more noisy check that assumes that functions can return 0. So there is no quick and simple solution imho.

    I think it would be a good idea to make Cppcheck more clever about functions return values. Perhaps you should be able to configure that for instance HalDaf_GetSectorSize() can return 0.

    Some standard functions such as strchr can return NULL. But I don't want to automatically guess that each strchr() return value is possibly NULL. I would like to guess that the return value can be NULL unless when the arguments mean that it can't be NULL. How we implement this .. I don't know.

    Do you know for sure that HalDaf_GetSectorSize() in your code can return zero? I don't know.. but it sounds to me that in general sector sizes can't be 0. Is there any way for Cppcheck to see that this could happen through whole program analysis etc?

     
  • michele

    michele - 2015-11-13

    Hello,
    thanks for your reply: I understand your point. I would like to be able to define a rule that every division by zero must be checked by and if divisor != 0, unless the divisor is a constant or a define (so the value is fixed). Is there a way to do that?
    Thanks for your support
    Michele

     
  • Daniel Marjamäki

    To start with.. can you try ublinter.

    https://github.com/danmar/ublinter

    let me know if you have problems etc..

    it is supposed to be more noisy.

    I would like to be able to define a rule that every division by zero must be checked by and if divisor != 0, unless the divisor is a constant or a define (so the value is fixed).

    I don't want to have such rule in the cppcheck tool. As an addon or extra rule it would be ok.

    Is there a way to do that?

    There is no quick and simple way.

    The way I would do this is to first improve the ValueFlow::Value so it can say for instance what the min/max values are also. The valueFlowAfterAssign() should set the min/max values. And the valueFlowForward() must probably have some handling for these.

    Then I would make sure that these min/max values can be read in the --dump output.

    Then I would write a addon that checks for division by zero using the min/max values.

    Updating the ValueFlow is the hard part. Improving the --dump output and writing the addon will then be trivial.

     
  • Daniel Marjamäki

    ublinter warns for your original code.

    divzero.c:

    void f() {
      int zBlockSize;
      int *pzNumBlocks;
    
      zBlockSize = HalDaf_GetSectorSize();
    
      /* evaluate number of sectors */
      if (pzNumBlocks != NULL) {
        *pzNumBlocks =( ( MemSize ) / zBlockSize );
      }
    }
    

    command line:

    $ cppcheck --dump divzero.c
    Checking divzero.c...
    $ python ublinter.py divzero.c.dump
    Checking divzero.c.dump...
    [divzero.c:10] Division by zero
    
     
  • michele

    michele - 2015-11-18

    Thanks a lot, I will have a look.
    I'm using cppcheck on my team and other colleagues are using instead klockwork. I'm checking issues not detected by cppcheck .. and viceversa. :)
    Thanks again for the support

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.