[rrd-developers] Re: rrd_diff patch

Tobias Oetiker oetiker at ee.ethz.ch
Fri Jun 27 22:37:07 MEST 2003


Jun 18 Luca Deri wrote:
Hi Luca,

I looked at the code again today, and I can not see why it should
crash. Here is why.

Moving the pointer ... (a--) is totally innocent, as long as I do
not read the memory where it is pointing at.

But exactly this, is what the code avoids doing:

       if (a1 >= a && b1 >= b) {
            *r1 = ((*a1 - c) - *b1) + '0';
        } else if (a1 >= a) {
            *r1 = (*a1 - c);
        } else {
            *r1 = ('0' - *b1 - c) + '0';
        }
        if (*r1 < '0') {
            *r1 += 10;
            c=1;
        } else
          if (*r1 > '9') { /* 0 - 10 */
            *r1 -= 10;
            c=1;
          } else {
            c=0;
        }


'a1' is initially pointing to the end of 'a'

it then gets moved towards the start of 'a'. The same happens for 'b'

as soon as 'a1' is pointing 'before' 'a', it is not being read
anymore ... the same is true for 'b1'

I have never the less replaced this code with:

       /* we want to avoid reading off the edge of the string */
        char save_a,save_b;
        save_a = ( a1 >= a) ? *a1 : '0';
        save_b = ( b1 >= b) ? *b1 : '0';
        *r1 = save_a - save_b - c + '0';
        if (*r1 < '0') {
            *r1 += 10;
            c=1;
        } else
          if (*r1 > '9') { /* 0 - 10 */
            *r1 -= 10;
            c=1;
          } else {
            c=0;
        }
        a1--;b1--;r1--;

because it seems easier to unserstand ...

cheers
tobi
-- 
 ______    __   _
/_  __/_  / /  (_) Oetiker @ ISG.EE, ETZ J97, ETH, CH-8092 Zurich
 / // _ \/ _ \/ /  System Manager, Time Lord, Coder, Designer, Coach
/_/ \.__/_.__/_/   http://people.ee.ethz.ch/~oetiker   +41(0)1-632-5286

--
Unsubscribe mailto:rrd-developers-request at list.ee.ethz.ch?subject=unsubscribe
Help        mailto:rrd-developers-request at list.ee.ethz.ch?subject=help
Archive     http://www.ee.ethz.ch/~slist/rrd-developers
WebAdmin    http://www.ee.ethz.ch/~slist/lsg2.cgi



More information about the rrd-developers mailing list