#2130 Endless loop in modulong.c


I am working on a large piece of code for a 18F2550. I have had it several times that the watchdog went off.
I was able to trace the problem to an endless loop in _modulong. I think this is the code in lib/pic16/libsdcc/long/modulong.c. The code for the pic14 target has an extra line with comments ("/* prevent endless loop (division by zero exception?!?) */") which the pic16 code does not have. Could this be the problem?


  • Raphael Neider

    Raphael Neider - 2013-01-15

    I "fixed" _modulong(a, b) to return ~0UL for b==0 instead of entering an infinite loop in r8388.

    However, if your program triggered this division-by-0 several times already, you may have to look into your code again: Division-by-0 triggers undefined behaviour as per the C standard (see, e.g., Clause in draft N1548), so that an endless loop is a perfectly valid implementation.


  • Raphael Neider

    Raphael Neider - 2013-01-15
    • assigned_to: nobody --> tecodev
    • milestone: --> non_bugs
    • status: open --> closed-fixed
  • Maarten Blomme

    Maarten Blomme - 2013-01-16

    I did some more debugging, and the original function call is a printf, but I don't know exactly with which parameters.
    If _modulong may not be called with b==0, there may be a problem somewhere between printf and _modulong.

    The problem is that I have a hard time reproducing this. My code prints the temperature every half second to the serial port, but the endless loop seems to happen at random. Sometimes it takes 9 hours, sometimes 1 hour.

    printf("Temp: %d\n\r", temperature);

  • Maarten Brock

    Maarten Brock - 2013-01-16

    I'd say that entering an infinite loop and optionally let a watchdog bite is preferable over just returning 0.


Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.

No, thanks