Skip to content

Commit

Permalink
2004-05-24 Sebastien Pouliot <sebastien@ximian.com>
Browse files Browse the repository at this point in the history
	* StringWriter.cs: Fixed possible integer overflow in Write. Changed
	spaces for tabs.

svn path=/trunk/mcs/; revision=27986
  • Loading branch information
Sebastien Pouliot committed May 24, 2004
1 parent 2785a19 commit 2740d6e
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 62 deletions.
2 changes: 2 additions & 0 deletions mcs/class/corlib/System.IO/ChangeLog
Expand Up @@ -3,6 +3,8 @@
* MemoryStream.cs: Fixed exception reporting to match MS Fx. Fixed the
condition to allow zeroization of existing data when we shrink the
stream.
* StringWriter.cs: Fixed possible integer overflow in Write. Changed
spaces for tabs.

2004-05-22 Duncan Mak <duncan@ximian.com>

Expand Down
136 changes: 74 additions & 62 deletions mcs/class/corlib/System.IO/StringWriter.cs
@@ -1,47 +1,56 @@
//
// System.IO.StringWriter
//
// Author: Marcin Szczepanski (marcins@zipworld.com.au)
// Authors
// Marcin Szczepanski (marcins@zipworld.com.au)
// Sebastien Pouliot <sebastien@ximian.com>
//
// Copyright (C) 2004 Novell (http://www.novell.com)
//


using System.Globalization;
using System.Text;

namespace System.IO {
[Serializable]

[Serializable]
public class StringWriter : TextWriter {

private StringBuilder internalString;
private bool disposed = false;

public StringWriter() : this (new StringBuilder ()) {

}

public StringWriter( IFormatProvider formatProvider ) : this (new StringBuilder (), formatProvider) {

}
public StringWriter () : this (new StringBuilder ())
{
}

public StringWriter( StringBuilder sb ) : this (sb, null) {
public StringWriter (IFormatProvider formatProvider) :
this (new StringBuilder (), formatProvider)
{
}

}
public StringWriter (StringBuilder sb) :
this (sb, null)
{
}

public StringWriter( StringBuilder sb, IFormatProvider formatProvider ) {
public StringWriter (StringBuilder sb, IFormatProvider formatProvider)
{
if (sb == null)
throw new ArgumentNullException ();
throw new ArgumentNullException ("sb");

internalString = sb;
internalFormatProvider = formatProvider;
}
internalString = sb;
internalFormatProvider = formatProvider;
}

public override System.Text.Encoding Encoding {
get {
return System.Text.Encoding.Unicode;
}
}
}

public override void Close() {
Dispose( true );
public override void Close ()
{
Dispose (true);
disposed = true;
}

Expand All @@ -53,51 +62,54 @@ protected override void Dispose (bool disposing)
disposed = true;
}

public virtual StringBuilder GetStringBuilder() {
return internalString;
}

public override string ToString() {
return internalString.ToString();
}

public override void Write( char value ) {

if (disposed)
throw new ObjectDisposedException ("StringWriter", "Cannot write to a closed StringWriter");

internalString.Append( value );
}

public override void Write( string value ) {

if (disposed)
throw new ObjectDisposedException ("StringWriter", "Cannot write to a closed StringWriter");
public virtual StringBuilder GetStringBuilder ()
{
return internalString;
}

internalString.Append( value );
public override string ToString ()
{
return internalString.ToString ();
}

public override void Write( char[] buffer, int index, int count ) {

if (disposed)
throw new ObjectDisposedException ("StringReader", "Cannot write to a closed StringWriter");
public override void Write (char value)
{
if (disposed) {
throw new ObjectDisposedException ("StringReader",
Locale.GetText ("Cannot write to a closed StringWriter"));
}

if( buffer == null ) {
throw new ArgumentNullException();
} else if( index < 0 || count < 0 ) {
throw new ArgumentOutOfRangeException();
} else if( index > buffer.Length || index + count > buffer.Length ) {
throw new ArgumentException();
}

char[] writeBuffer = new char[ count ];
internalString.Append (value);
}

Array.Copy( buffer, index, writeBuffer, 0, count );
public override void Write (string value)
{
if (disposed) {
throw new ObjectDisposedException ("StringReader",
Locale.GetText ("Cannot write to a closed StringWriter"));
}

internalString.Append (value);
}

internalString.Append( writeBuffer );
}

}
public override void Write (char[] buffer, int index, int count)
{
if (disposed) {
throw new ObjectDisposedException ("StringReader",
Locale.GetText ("Cannot write to a closed StringWriter"));
}
if (buffer == null)
throw new ArgumentNullException ("buffer");
if (index < 0)
throw new ArgumentOutOfRangeException ("index", "< 0");
if (count < 0)
throw new ArgumentOutOfRangeException ("count", "< 0");
// re-ordered to avoid possible integer overflow
if (index > buffer.Length - count)
throw new ArgumentException ("index + count > buffer.Length");

internalString.Append (buffer, index, count);
}
}
}



0 comments on commit 2740d6e

Please sign in to comment.