Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid return new MemoryStream, instead return null and manage nulls i… #661

Merged
merged 11 commits into from
Sep 22, 2022
Merged
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,9 @@ build
/dotnet/src/dotnetcore/GxDataInitialization/net6.0/GXDataInitialization.deps.json
/dotnet/src/dotnetcore/GxNetCoreStartup/net6.0/GxNetCoreStartup.deps.json
/dotnet/src/dotnetcore/Reor/net6.0/Reor.deps.json
.out

/dotnet/outsonar.txt
/dotnet/output.txt
/dotnet/Veracode.sln
/dotnet/VeracodeSolution.sln

10 changes: 9 additions & 1 deletion dotnet/src/dotnetcore/GxClasses/Domain/GXXmlReadWrite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,15 @@ public short OpenResponse(IGxHttpClient httpClient)
Close();
EntitiesContainer.Reset();
CreateXMLSettings();
XMLInput = httpClient.ReceiveStream;
GxHttpClient gxHttpClient = httpClient as GxHttpClient;
if (gxHttpClient!=null && gxHttpClient.ReceiveData != null)
{
XMLInput = new MemoryStream(gxHttpClient.ReceiveData);
}
else
{
XMLInput = string.Empty;
}
return 0;
}

Expand Down
1 change: 0 additions & 1 deletion dotnet/src/dotnetcore/GxClasses/GxClasses.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@
<Folder Include="Domain\SD\" />
<Folder Include="Attributes\" />
<Folder Include="Configuration\" />
<Folder Include="Helpers\Cryptography\Encryption\" />
<Folder Include="Helpers\Cryptography\Signing\Standards\" />
<Folder Include="Helpers\Process\" />
<Folder Include="Services\CoreWebAPI\Helpers\" />
Expand Down

This file was deleted.

1 change: 0 additions & 1 deletion dotnet/src/dotnetcore/GxMail/GxMail.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@
<PackageReference Include="MimeKit" Version="3.1.1" />
<PackageReference Include="Microsoft.Identity.Client" Version="4.42.0" />
<PackageReference Include="OpenPop" Version="2.0.6.2" />
<PackageReference Include="MailKit" Version="3.1.1" />
<PackageReference Include="Org.Mentalis.Security" Version="1.0.0" />
</ItemGroup>

Expand Down
15 changes: 12 additions & 3 deletions dotnet/src/dotnetframework/GxClasses/Core/GXUtilsCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5486,14 +5486,18 @@ private static Bitmap BitmapCreateFromStream(string filePathOrUrl)
{
using (Stream s = ImageFile(filePathOrUrl).GetStream())
{
return new Bitmap(s);
if (s != null)
return new Bitmap(s);
return null;
}
}
private static Image ImageCreateFromStream(string filePathOrUrl)
{
using (Stream s = ImageFile(filePathOrUrl).GetStream())
{
return Image.FromStream(s);
if (s != null)
return Image.FromStream(s);
return null;
}
}

Expand All @@ -5508,6 +5512,8 @@ public static string Resize(string imageFile, int width, int height, bool keepAs
{
using (Image image = ImageCreateFromStream(imageFile))
{
if (image == null)
return string.Empty;
int newheight = height;
// Prevent using images internal thumbnail
image.RotateFlip(RotateFlipType.Rotate180FlipNone);
Expand Down Expand Up @@ -5667,7 +5673,10 @@ public static int GetImageWidth(string imageFile)
{
using (Bitmap bmp = BitmapCreateFromStream(imageFile))
{
return bmp.Width;
if (bmp != null)
{
return bmp.Width;
}
}
}
catch (Exception ex)
Expand Down
22 changes: 18 additions & 4 deletions dotnet/src/dotnetframework/GxClasses/Core/Web/GxHttpServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace GeneXus.Http.Server
using System.Net.Http;
using Stubble.Core.Contexts;
using System.Net.Mime;

#endif

public class GxHttpCookie
Expand Down Expand Up @@ -394,17 +395,30 @@ public string GetValue( string name)
return string.Empty;
}
}
// create function to convert stream to string

#if NETCORE
const int StreamReaderDefaultBufferSize = -1;
#else
const int StreamReaderDefaultBufferSize = 1024;
#endif
private string GetStringFromStream(Stream stream)
{
if (stream == null)
return string.Empty;
using (StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8, true, StreamReaderDefaultBufferSize, true))
{
return reader.ReadToEnd();
}
}
public override string ToString()
{
if (_httpReq == null)
return String.Empty;
#if NETCORE
return _httpReq.GetRawBodyString();
#else
using (StreamReader reader = new StreamReader(_httpReq.InputStream))
{
return reader.ReadToEnd();
}
return GetStringFromStream(_httpReq.InputStream);
#endif
}
public void ToFile(string FileName)
Expand Down
13 changes: 1 addition & 12 deletions dotnet/src/dotnetframework/GxClasses/Data/GXDataMemory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -871,18 +871,7 @@ public static object GetCopyFrom(object value)
{
MemoryStream memStream = new MemoryStream();
Stream oStream = (Stream)value;
byte[] buffer = new byte[4096];

oStream.Seek(0, SeekOrigin.Begin);

int len;
do
{
len = oStream.Read(buffer, 0, buffer.Length);
memStream.Write(buffer, 0, len);
}
while (len > 0);

oStream.CopyTo(memStream);
return memStream;
}
else
Expand Down
48 changes: 25 additions & 23 deletions dotnet/src/dotnetframework/GxClasses/Data/GXDataPostgreSQL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -361,33 +361,35 @@ private byte[] ByteaTextToByteArray(byte[] BackendData)
}
else
{
MemoryStream ms = new MemoryStream();
while (byteAPosition < byteALength)
using (MemoryStream ms = new MemoryStream())
{
byte octalValue;
byte b = BackendData[byteAPosition];
if (b >= 0x20 && b < 0x7F && b != 0x27 && b != 0x5C)
while (byteAPosition < byteALength)
{
octalValue = BackendData[byteAPosition];
byteAPosition++;
ms.WriteByte((Byte)octalValue);
}
else if (BackendData[byteAPosition] == ASCIIByteBackSlash &&
byteAPosition + 3 < byteALength &&
BackendData[byteAPosition + 1] >= ASCIIByteb0 && BackendData[byteAPosition + 1] <= ASCIIByteb7 &&
BackendData[byteAPosition + 2] >= ASCIIByteb0 && BackendData[byteAPosition + 2] <= ASCIIByteb7 &&
BackendData[byteAPosition + 3] >= ASCIIByteb0 && BackendData[byteAPosition + 3] <= ASCIIByteb7)
{
octalValue = FastConverter.ToByteEscapeFormat(BackendData, byteAPosition + 1);
byteAPosition += 4;
ms.WriteByte((Byte)octalValue);
}
else
{
return BackendData;
byte octalValue;
byte b = BackendData[byteAPosition];
if (b >= 0x20 && b < 0x7F && b != 0x27 && b != 0x5C)
{
octalValue = BackendData[byteAPosition];
byteAPosition++;
ms.WriteByte((Byte)octalValue);
}
else if (BackendData[byteAPosition] == ASCIIByteBackSlash &&
byteAPosition + 3 < byteALength &&
BackendData[byteAPosition + 1] >= ASCIIByteb0 && BackendData[byteAPosition + 1] <= ASCIIByteb7 &&
BackendData[byteAPosition + 2] >= ASCIIByteb0 && BackendData[byteAPosition + 2] <= ASCIIByteb7 &&
BackendData[byteAPosition + 3] >= ASCIIByteb0 && BackendData[byteAPosition + 3] <= ASCIIByteb7)
{
octalValue = FastConverter.ToByteEscapeFormat(BackendData, byteAPosition + 1);
byteAPosition += 4;
ms.WriteByte((Byte)octalValue);
}
else
{
return BackendData;
}
}
return ms.ToArray();
}
return ms.ToArray();
}
}catch(Exception ex)
{
Expand Down
18 changes: 10 additions & 8 deletions dotnet/src/dotnetframework/GxClasses/Domain/GXFileIO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,10 @@ public void MoveTo(string desDirName)
public class GxFileInfo : IGxFileInfo
{
private static readonly ILog log = log4net.LogManager.GetLogger(typeof(GxFileInfo));
FileInfo _file;
string _baseDirectory;
private FileInfo _file;
private string _baseDirectory;

public GxFileInfo(FileInfo file)
public GxFileInfo(FileInfo file)
{
_file = file;
}
Expand Down Expand Up @@ -657,7 +657,7 @@ public class GxFile
private static readonly ILog log = log4net.LogManager.GetLogger(typeof(GxFile));

internal IGxFileInfo _file;
string _baseDirectory;
string _baseDirectory;
int _lastError;
string _lastErrorDescription;
string _source;
Expand Down Expand Up @@ -1040,11 +1040,11 @@ public Stream GetStream()
_lastError = 0;
_lastErrorDescription = "";
if (!validSource())
return new MemoryStream();
return null;
if (!Exists())
{
_lastError = 2;
return new MemoryStream();
return null;
}
try
{
Expand All @@ -1054,7 +1054,7 @@ public Stream GetStream()
{
setError(e);
}
return new MemoryStream();
return null;
}
public bool PathIsRooted()
{
Expand Down Expand Up @@ -1437,7 +1437,7 @@ public void Open(String encoding)
private FileStream _fileStreamReader;
private StreamReader _fileReader;

public void OpenWrite(String encoding)
public void OpenWrite(String encoding)
{
_lastError = 0;
_lastErrorDescription = "";
Expand Down Expand Up @@ -1568,6 +1568,8 @@ public void Close()
}
}
}


}

public class GxDirectory
Expand Down
16 changes: 15 additions & 1 deletion dotnet/src/dotnetframework/GxClasses/Domain/GXXmlReadWrite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class GXXMLReader : IDisposable
public const int ValidationXDR = 4;

private XmlTextReader treader;
private MemoryStream mreader;
private XmlValidatingReader vreader;


Expand Down Expand Up @@ -229,7 +230,14 @@ public short OpenResponse(IGxHttpClient httpClient)
{
if (treader != null) Close();
EntitiesContainer.Reset();
treader = new XmlTextReader(httpClient.ReceiveStream);
GxHttpClient gxHttpClient = httpClient as GxHttpClient;
if (gxHttpClient != null && gxHttpClient.ReceiveData != null)
{
mreader = new MemoryStream(gxHttpClient.ReceiveData);
treader = new XmlTextReader(mreader);
}
else
treader = new XmlTextReader(string.Empty);
if (treader != null)
{
SetDtdProcessing(treader, Resolver, validationType);
Expand Down Expand Up @@ -290,6 +298,7 @@ public void OpenFromString(string s)
Uri baseUri = new Uri( sBaseDirectory );
Resolver.Myself = baseUri;
treader = null;
mreader = null;
try
{
if (File.Exists(s))
Expand Down Expand Up @@ -629,6 +638,11 @@ public short Close()
treader.Close();
treader = null;
}
if (mreader != null)
{
mreader.Close();
mreader = null;
}
if ( vreader != null )
{
vreader.Close();
Expand Down
Loading