tencodingerrors.pp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. program tencodingerrors;
  2. {$mode delphi}{$H+}
  3. uses
  4. SysUtils;
  5. var
  6. S: String;
  7. Bytes: TBytes;
  8. begin
  9. S := '';
  10. SetLength(Bytes, 0);
  11. try
  12. // invalid source array?
  13. TEncoding.UTF8.GetBytes(S, 1, -1, Bytes, 0);
  14. halt(1);
  15. except on E: Exception do
  16. WriteLn(E.ClassName, ' ', E.Message);
  17. end;
  18. S := 'Test';
  19. try
  20. // delphi raises a message "Invalid source array" while the problem is in
  21. // destination array in real
  22. TEncoding.UTF8.GetBytes(S, 0, 2, Bytes, 0);
  23. halt(2);
  24. except on E: Exception do
  25. WriteLn(E.ClassName, ' ', E.Message);
  26. end;
  27. SetLength(Bytes, 1);
  28. try
  29. // invalid count
  30. TEncoding.UTF8.GetBytes(S, 5, 2, Bytes, 0);
  31. halt(3);
  32. except on E: Exception do
  33. WriteLn(E.ClassName, ' ', E.Message);
  34. end;
  35. try
  36. // character index out of bounds
  37. TEncoding.UTF8.GetBytes(S, 0, 2, Bytes, 0);
  38. halt(4);
  39. except on E: Exception do
  40. WriteLn(E.ClassName, ' ', E.Message);
  41. end;
  42. try
  43. // invalid destination index
  44. TEncoding.UTF8.GetBytes(S, 1, 2, Bytes, -1);
  45. halt(5);
  46. except on E: Exception do
  47. WriteLn(E.ClassName, ' ', E.Message);
  48. end;
  49. end.