sdk icon indicating copy to clipboard operation
sdk copied to clipboard

[analyzer/ffi] Analyzer reporting false positives for NativeFieldWrapperClass1

Open dcharkes opened this issue 3 years ago • 1 comments
trafficstars

The analyzer reports an error on the following two cases.

/// Subtracts 2 integers.
@ffi.FfiNative<ffi.Int Function(ffi.Pointer<ffi.Int>, ffi.Int)>('subtract')
external int subtract(
  ffi.Pointer<ffi.Int> a,
  int b,
);

/// Divides 2 floats, returns a pointer to double.
@ffi.FfiNative<
    ffi.Pointer<ffi.Double> Function(
        ffi.Pointer<ffi.Float>, ffi.Pointer<ffi.Float>)>('dividePrecision')
external ffi.Pointer<ffi.Double> dividePrecision(
  ffi.Pointer<ffi.Float> a,
  ffi.Pointer<ffi.Float> b,
);
  • Only classes extending NativeFieldWrapperClass1 can be passed as Pointer. Pass as Handle instead.

This is a bit too zealous. These are not implicitly passing this, they have the same amount of Dart parameters as C types and are top level functions.

Discovered in https://github.com/dart-lang/ffigen/pull/447.

dcharkes avatar Aug 17 '22 10:08 dcharkes

This is causing the CI of ffigen to go red: https://github.com/dart-lang/ffigen/actions/runs/3029924189/jobs/4875652903

dcharkes avatar Sep 12 '22 19:09 dcharkes

Also:

@FfiNative<Void Function(Pointer)>('free')
external void posixFree(Pointer pointer);

@FfiNative<Void Function(Pointer)>('CoTaskMemFree')
external void winCoTaskMemFree(Pointer pv);

dcharkes avatar Sep 27 '22 14:09 dcharkes