ext: avoid potentially unsafe casts with C API