[cfe/ffi] Fix division by 0 on `@Packed(0)`

Fixes: https://github.com/dart-lang/sdk/issues/45838

TEST=tests/ffi/vmspecific_static_checks_test.dart

(Looks like `dart format` also slightly changed. Committing such that
the files are in line with the formatter again.)

Change-Id: Iefd8af8c38a7490175b2e25b46cedbf85f15f17d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/197340
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Tess Strickland <sstrickl@google.com>
diff --git a/pkg/vm/lib/transformations/ffi_definitions.dart b/pkg/vm/lib/transformations/ffi_definitions.dart
index 9225204..def3799 100644
--- a/pkg/vm/lib/transformations/ffi_definitions.dart
+++ b/pkg/vm/lib/transformations/ffi_definitions.dart
@@ -335,10 +335,8 @@
             }
             for (var dimension in dimensions) {
               if (dimension < 0) {
-                diagnosticReporter.report(
-                    messageNonPositiveArrayDimensions, f.fileOffset,
-                    f.name.text.length,
-                    f.fileUri);
+                diagnosticReporter.report(messageNonPositiveArrayDimensions,
+                    f.fileOffset, f.name.text.length, f.fileUri);
                 success = false;
               }
             }
@@ -489,8 +487,8 @@
         final sizeAnnotations = _getArraySizeAnnotations(m).toList();
         if (sizeAnnotations.length == 1) {
           final arrayDimensions = sizeAnnotations.single;
-          type = NativeTypeCfe(this, dartType, compoundCache: compoundCache,
-              arrayDimensions: arrayDimensions);
+          type = NativeTypeCfe(this, dartType,
+              compoundCache: compoundCache, arrayDimensions: arrayDimensions);
         }
       } else if (isPointerType(dartType) || isCompoundSubtype(dartType)) {
         type = NativeTypeCfe(this, dartType, compoundCache: compoundCache);
@@ -760,7 +758,7 @@
 abstract class NativeTypeCfe {
   factory NativeTypeCfe(FfiTransformer transformer, DartType dartType,
       {List<int> arrayDimensions,
-        Map<Class, CompoundNativeTypeCfe> compoundCache = const {}}) {
+      Map<Class, CompoundNativeTypeCfe> compoundCache = const {}}) {
     if (transformer.isPrimitiveType(dartType)) {
       final clazz = (dartType as InterfaceType).classNode;
       final nativeType = transformer.getType(clazz);
@@ -783,7 +781,7 @@
       }
       final elementType = transformer.arraySingleElementType(dartType);
       final elementCfeType =
-      NativeTypeCfe(transformer, elementType, compoundCache: compoundCache);
+          NativeTypeCfe(transformer, elementType, compoundCache: compoundCache);
       return ArrayNativeTypeCfe.multi(elementCfeType, arrayDimensions);
     }
     throw "Invalid type $dartType";
@@ -1102,7 +1100,9 @@
       if (packing != null && packing < alignment) {
         alignment = packing;
       }
-      offset = _alignOffset(offset, alignment);
+      if (alignment > 0) {
+        offset = _alignOffset(offset, alignment);
+      }
       offsets.add(offset);
       offset += size;
       structAlignment = math.max(structAlignment, alignment);
@@ -1146,8 +1146,8 @@
 
   ArrayNativeTypeCfe(this.elementType, this.length);
 
-  factory ArrayNativeTypeCfe.multi(NativeTypeCfe elementType,
-      List<int> dimensions) {
+  factory ArrayNativeTypeCfe.multi(
+      NativeTypeCfe elementType, List<int> dimensions) {
     if (dimensions.length == 1) {
       return ArrayNativeTypeCfe(elementType, dimensions.single);
     }
diff --git a/tests/ffi/vmspecific_static_checks_test.dart b/tests/ffi/vmspecific_static_checks_test.dart
index 4998b31..a89638c 100644
--- a/tests/ffi/vmspecific_static_checks_test.dart
+++ b/tests/ffi/vmspecific_static_checks_test.dart
@@ -761,6 +761,11 @@
       nestedLooselyPacked; //# 1606: compile-time error
 }
 
+@Packed(0) //# 1607: compile-time error
+class TestStruct1607 extends Struct {
+  external Pointer<Uint8> notEmpty;
+}
+
 class TestStruct1800 extends Struct {
   external Pointer<Uint8> notEmpty;
 
@@ -780,4 +785,4 @@
 
   @Array.multi([2, 2, 2, 2, 2, 2, -1]) //# 1802: compile-time error
   external Array<Uint8> inlineArray; //# 1802: compile-time error
-}
\ No newline at end of file
+}
diff --git a/tests/ffi_2/vmspecific_static_checks_test.dart b/tests/ffi_2/vmspecific_static_checks_test.dart
index a81e206..ae19a0d 100644
--- a/tests/ffi_2/vmspecific_static_checks_test.dart
+++ b/tests/ffi_2/vmspecific_static_checks_test.dart
@@ -760,6 +760,11 @@
   Array<TestStruct1604> nestedLooselyPacked; //# 1606: compile-time error
 }
 
+@Packed(0) //# 1607: compile-time error
+class TestStruct1607 extends Struct {
+  Pointer<Uint8> notEmpty;
+}
+
 class TestStruct1800 extends Struct {
   Pointer<Uint8> notEmpty;
 
@@ -779,4 +784,4 @@
 
   @Array.multi([2, 2, 2, 2, 2, 2, -1]) //# 1802: compile-time error
   Array<Uint8> inlineArray; //# 1802: compile-time error
-}
\ No newline at end of file
+}