Search Unity

Compiler bug?

Discussion in 'iOS and tvOS' started by orangetech, Nov 15, 2021.

  1. orangetech

    orangetech

    Joined:
    Sep 30, 2017
    Posts:
    50
    out parameter repoter "Use of unassigned local variable 'vrmb62'" when build xcode project, but fine in editor.

    here is test code:
    Code (CSharp):
    1. public string SourcrFile = @"R:\M99o6kLkDRA0_a4U8nF_HngQnk8-eDKJzLL5OlMk1VQ.vrm";
    2.  
    3.   void Start()
    4.   {
    5.     Debug.Assert(GenerateSHA256(SourcrFile, out byte[] vrmb64, out string vrmb62));//Error on iOS build, good on Editor
    6.  
    7.     //bool succeed = GenerateSHA256(SourcrFile, out byte[] vrmb64, out string vrmb62);//good on iOS build, good on Editor
    8.     //Debug.Assert(succeed);
    9.  
    10.     Debug.Log(vrmb62);
    11.   }
    12.  
    13.   public static bool GenerateSHA256(string filePath, out byte[] b64, out string b62)
    14.   {
    15.     if (!File.Exists(filePath))
    16.     {
    17.       Debug.LogError("GenerateSHA256:File doesn't exist");
    18.       b64 = null; b62 = null;
    19.       return false;
    20.     }
    21.  
    22.     using (SHA256Managed sha256 = new SHA256Managed())
    23.     {
    24.       using (FileStream fileStream = File.OpenRead(filePath))
    25.       {
    26.         b64 = sha256.ComputeHash(fileStream);
    27.         b62 = UrlBase64.Encode(b64);
    28.         return true;
    29.       }
    30.     }
    31.   }
    32.   public enum PaddingPolicy
    33.   {
    34.     Discard,
    35.     Preserve,
    36.   }
    37.   public static class UrlBase64
    38.   {
    39.     private static readonly char[] TwoPads = { '=', '=' };
    40.  
    41.     public static string Encode(byte[] bytes, PaddingPolicy padding = PaddingPolicy.Discard)
    42.     {
    43.       var encoded = Convert.ToBase64String(bytes).Replace('+', '-').Replace('/', '_');
    44.       if (padding == PaddingPolicy.Discard)
    45.       {
    46.         encoded = encoded.TrimEnd('=');
    47.       }
    48.  
    49.       return encoded;
    50.     }
    51.  
    52.     public static byte[] Decode(string encoded)
    53.     {
    54.       var chars = new List<char>(encoded.ToCharArray());
    55.  
    56.       for (int i = 0; i < chars.Count; ++i)
    57.       {
    58.         if (chars[i] == '_')
    59.         {
    60.           chars[i] = '/';
    61.         }
    62.         else if (chars[i] == '-')
    63.         {
    64.           chars[i] = '+';
    65.         }
    66.       }
    67.  
    68.       switch (encoded.Length % 4)
    69.       {
    70.         case 2:
    71.           chars.AddRange(TwoPads);
    72.           break;
    73.         case 3:
    74.           chars.Add('=');
    75.           break;
    76.       }
    77.  
    78.       var array = chars.ToArray();
    79.  
    80.       return Convert.FromBase64CharArray(array, 0, array.Length);
    81.     }
    82.   }
     
  2. Neonlyte

    Neonlyte

    Joined:
    Oct 17, 2013
    Posts:
    516
    This seemed to be the correct behavior. Assertions(Debug.Assert) will be removed during compilation if you build in Release mode (which is the default mode when exporting). Because of the removal, the only line remaining in that function would be the Debug.Log, and thus your function is now exiting the scope without fully assigning the out parameter, and thus the error.

    The correct way to do the assertion in your case is the commented out lines.
     
  3. orangetech

    orangetech

    Joined:
    Sep 30, 2017
    Posts:
    50
    Tank you, Solved my doubts