2
0
Эх сурвалжийг харах

* avoid range check error when using SwapEndian with 16-bit constants
+ added test

git-svn-id: trunk@46897 -

svenbarth 4 жил өмнө
parent
commit
9d86fed95b

+ 1 - 0
.gitattributes

@@ -13349,6 +13349,7 @@ tests/tbs/tb0672.pp svneol=native#text/pascal
 tests/tbs/tb0673.pp svneol=native#text/pascal
 tests/tbs/tb0674.pp svneol=native#text/pascal
 tests/tbs/tb0675.pp svneol=native#text/pascal
+tests/tbs/tb0676.pp svneol=native#text/pascal
 tests/tbs/ub0060.pp svneol=native#text/plain
 tests/tbs/ub0069.pp svneol=native#text/plain
 tests/tbs/ub0119.pp svneol=native#text/plain

+ 2 - 2
rtl/arm/arm.inc

@@ -1190,13 +1190,13 @@ function SwapEndian(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inlin
     { is turned into "longint(AValue) shr 8", so if AValue < 0 then    }
     { the sign bits from the upper 16 bits are shifted in rather than  }
     { zeroes.                                                          }
-    Result := SmallInt((Word(AValue) shr 8) or (Word(AValue) shl 8));
+    Result := ((Word(AValue) shr 8) or (Word(AValue) shl 8)) and $ffff;
   end;
 
 
 function SwapEndian(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
   begin
-    Result := Word((AValue shr 8) or (AValue shl 8));
+    Result := ((AValue shr 8) or (AValue shl 8)) and $ffff;
   end;
 
 (*

+ 2 - 2
rtl/inc/generic.inc

@@ -2663,13 +2663,13 @@ function SwapEndian(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inlin
     { is turned into "longint(AValue) shr 8", so if AValue < 0 then    }
     { the sign bits from the upper 16 bits are shifted in rather than  }
     { zeroes.                                                          }
-    Result := SmallInt((Word(AValue) shr 8) or (Word(AValue) shl 8));
+    Result := ((Word(AValue) shr 8) or (Word(AValue) shl 8)) and $ffff;
   end;
 
 {$ifndef cpujvm}
 function SwapEndian(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
   begin
-    Result := Word((AValue shr 8) or (AValue shl 8));
+    Result := ((AValue shr 8) or (AValue shl 8)) and $ffff;
   end;
 {$endif}
 

+ 2 - 2
rtl/x86_64/x86_64.inc

@@ -1021,13 +1021,13 @@ function SwapEndian(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inlin
     { is turned into "longint(AValue) shr 8", so if AValue < 0 then    }
     { the sign bits from the upper 16 bits are shifted in rather than  }
     { zeroes.                                                          }
-    Result := SmallInt((Word(AValue) shr 8) or (Word(AValue) shl 8));
+    Result := ((Word(AValue) shr 8) or (Word(AValue) shl 8)) and $ffff;
   end;
 
 
 function SwapEndian(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
   begin
-    Result := Word((AValue shr 8) or (AValue shl 8));
+    Result := ((AValue shr 8) or (AValue shl 8)) and $ffff;
   end;
 
 

+ 14 - 0
tests/tbs/tb0676.pp

@@ -0,0 +1,14 @@
+{ %NORUN }
+
+program tb0676;
+
+{$warn 4110 error}
+
+begin
+  SwapEndian(UInt16($1234));
+  SwapEndian(Int16($8765));
+  SwapEndian(UInt32($12345678));
+  SwapEndian(Int32($87654321));
+  SwapEndian(UInt64($1234567887654321));
+  SwapEndian(Int64($8765432112345678));
+end.