tgingold-cern / cheby

GNU General Public License v3.0
8 stars 4 forks source link

Fix crash in maybe_pad due to missing el, make parameters explicit #20

Closed stefanlippuner closed 1 year ago

stefanlippuner commented 1 year ago

Changes:

On master, generating a C file for the test file results in the following crash in maybe_pad:

cheby-dev -i reg.cheby --gen-c reg.h
# [...]
  File "proto/cheby/tree.py", line 32, in visit
    return f(*args, **kwargs)
  File "proto/cheby/gen_c.py", line 125, in cprint_block
    cprint_children(cp, n, n.c_size)
  File "proto/cheby/gen_c.py", line 100, in cprint_children
    maybe_pad(size - addr)
  File "proto/cheby/gen_c.py", line 69, in maybe_pad
    cp.cp_txt('/* padding to: {} words */'.format(el.c_address // sz))
NameError: free variable 'el' referenced before assignment in enclosing scope

The bug is triggered when n.c_sorted_children is empty. In that case, el is not defined before it's used for the last pad.

The padding comments referred to the number of words of the specific size, which could be quite confusing. E.g., the following could happen:

    /* padding to: 28 words */
    uint8_t __padding_1[2];

    /* ... */

    /* padding to: 16 words */
    uint32_t __padding_0[5];

now this should be more clear:

    /* padding to: 28 Bytes */
    uint8_t __padding_1[2];

    /* ... */

    /* padding to: 64 Bytes */
    uint32_t __padding_0[5];
tgingold-cern commented 1 year ago

Thanks!