You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There's an optimization for memory stream in EncodedStringText.Decode, which retrieves the backing byte array if it can. This optimization does not account for a MemoryStream that was constructed with a non-zero offset. The fix should use MemoryStream.TryGetBuffer instead of MemoryStream.GetBuffer in order to get an array segment with the appropriate offset.
Steps to Reproduce:
Compile and run the following program.
(I'm not sure if the root issue in EncodedStringText.Decode can affect anything other than scripting. This was the first code path I found where the user could pass an arbitrary stream.)
usingMicrosoft.CodeAnalysis;usingMicrosoft.CodeAnalysis.CSharp.Scripting;usingMicrosoft.CodeAnalysis.Scripting;usingSystem.Diagnostics;usingSystem.IO;usingSystem.Linq;usingSystem.Text;classResolver:SourceReferenceResolver{publicoverrideboolEquals(objectother)=>ReferenceEquals(this,other);publicoverrideintGetHashCode()=>42;publicoverridestringResolveReference(stringpath,stringbaseFilePath)=>path;publicoverridestringNormalizePath(stringpath,stringbaseFilePath)=>path;publicoverrideStreamOpenRead(stringresolvedPath){// Make an ASCII text buffer with Hello World script preceded by padding Qsconstintpadding=42;stringtext=@"System.Console.WriteLine(""Hello World!"");";byte[]bytes=Enumerable.Repeat((byte)'Q',text.Length+padding).ToArray();Encoding.ASCII.GetBytes(text,0,text.Length,bytes,padding);// Make a stream over the program portion, skipping the Qs.varstream=newMemoryStream(bytes,padding,text.Length,writable:false,publiclyVisible:true);// sanity check that reading entire stream gives us back our text.using(varstreamReader=newStreamReader(stream,Encoding.ASCII,detectEncodingFromByteOrderMarks:false,bufferSize:bytes.Length,leaveOpen:true)){vartextFromStream=streamReader.ReadToEnd();Debug.Assert(textFromStream==text);}stream.Position=0;returnstream;}}classProgram{staticvoidMain(string[]args){varoptions=ScriptOptions.Default.WithSourceResolver(newResolver());varscript=CSharpScript.Create(@"#load ""foo.cs""",options);varresult=script.RunAsync().Result;}}
Expected Behavior:
Script runs and prints hello world.
Actual Behavior:
Unhandled Exception: Microsoft.CodeAnalysis.Scripting.CompilationErrorException: foo.cs(1,1): error CS0103: The name 'QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ' does not exist in the current context
The text was updated successfully, but these errors were encountered:
@nguerrera It does? I don't see anything similar to #12408 in #12353 and running the above code when referencing code from #12353 still throws the exception for me.
Version Used: 2.0 (Synced to master recently)
There's an optimization for memory stream in EncodedStringText.Decode, which retrieves the backing byte array if it can. This optimization does not account for a MemoryStream that was constructed with a non-zero offset. The fix should use MemoryStream.TryGetBuffer instead of MemoryStream.GetBuffer in order to get an array segment with the appropriate offset.
Steps to Reproduce:
Compile and run the following program.
(I'm not sure if the root issue in EncodedStringText.Decode can affect anything other than scripting. This was the first code path I found where the user could pass an arbitrary stream.)
Expected Behavior:
Script runs and prints hello world.
Actual Behavior:
Unhandled Exception: Microsoft.CodeAnalysis.Scripting.CompilationErrorException: foo.cs(1,1): error CS0103: The name 'QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ' does not exist in the current context
The text was updated successfully, but these errors were encountered: