dstep icon indicating copy to clipboard operation
dstep copied to clipboard

Pointer arithmetic and parentheses elision

Open jblachly opened this issue 3 years ago • 0 comments

This was caught by our unit tests. Parentheses elision in #define macro to template conversion can lead to incorrect results when dealing with pointers.

This line:

#define bam_get_cigar(b) ((uint32_t*)((b)->data + (b)->core.l_qname))

Was translated by dstep to:

extern (D) auto bam_get_cigar(T)(auto ref T b)
{
    return cast(uint*) b.data + b.core.l_qname;
}

Pointer arithmetic rules cause this to give wrong result. Our original hand translation preserves parentheses, giving correct results:

extern (D) auto bam_get_cigar(bam1_t * b)
{
    return cast(uint*) ((*b).data + (*b).core.l_qname);
}

(b->data here is a ubyte *)

noted by @charlesgregory

jblachly avatar Aug 26 '20 19:08 jblachly