Skip to content

Commit

Permalink
Fix #119
Browse files Browse the repository at this point in the history
  • Loading branch information
ironfede committed Mar 2, 2024
1 parent e9c2e4e commit d18fe49
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 13 deletions.
2 changes: 1 addition & 1 deletion sources/OpenMcdf.Extensions/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("-")]
[assembly: AssemblyProduct("OpenMcdf.Extensions")]
[assembly: AssemblyCopyright("Copyright © Federico Blaseotto, 2023")]
[assembly: AssemblyCopyright("Copyright © Federico Blaseotto, 2024")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

Expand Down
6 changes: 4 additions & 2 deletions sources/OpenMcdf/CompoundFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2269,10 +2269,12 @@ internal void SetStreamLength(CFItem cfItem, long length)

int nSec = (int)Math.Floor(((double)(Math.Abs(delta)) / newSectorSize)); //number of sectors to mark as free

int startFreeSector = sectorChain.Count - nSec; // start sector to free

if (newSectorSize == Sector.MINISECTOR_SIZE)
FreeMiniChain(sectorChain, nSec, this.eraseFreeSectors);
FreeMiniChain(sectorChain, startFreeSector, this.eraseFreeSectors);
else
FreeChain(sectorChain, nSec, this.eraseFreeSectors);
FreeChain(sectorChain, startFreeSector, this.eraseFreeSectors);
}

if (sectorChain.Count > 0)
Expand Down
2 changes: 1 addition & 1 deletion sources/OpenMcdf/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Federico Blaseotto")]
[assembly: AssemblyProduct("OpenMcdf 2.3")]
[assembly: AssemblyCopyright("Copyright © 2010-2023, Federico Blaseotto")]
[assembly: AssemblyCopyright("Copyright © 2010-2024, Federico Blaseotto")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

Expand Down
62 changes: 53 additions & 9 deletions sources/Test/OpenMcdf.Test/CFSStreamTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -809,9 +809,12 @@ public void Test_APPEND_DATA_TO_CREATE_LARGE_STREAM()
[TestMethod]
public void Test_RESIZE_STREAM_NO_TRANSITION()
{
int INITIAL_SIZE = 1024 * 1024 * 2;
int DELTA_SIZE = 300;

CompoundFile cf = null;
//CFStream st = null;
byte[] b = Helpers.GetBuffer(1024 * 1024 * 2); //2MB buffer
byte[] b = Helpers.GetBuffer(INITIAL_SIZE); //2MB buffer

cf = new CompoundFile(CFSVersion.Ver_3, CFSConfiguration.Default);
cf.RootStorage.AddStream("AStream").SetData(b);
Expand All @@ -820,10 +823,22 @@ public void Test_RESIZE_STREAM_NO_TRANSITION()

cf = new CompoundFile("$Test_RESIZE_STREAM.cfs", CFSUpdateMode.Update, CFSConfiguration.SectorRecycle);
CFStream item = cf.RootStorage.GetStream("AStream");
item.Resize(item.Size / 2);
item.Resize(INITIAL_SIZE - DELTA_SIZE);
//cf.RootStorage.AddStream("BStream").SetData(b);
cf.Commit(true);
cf.Close();


cf = new CompoundFile("$Test_RESIZE_STREAM.cfs", CFSUpdateMode.ReadOnly, CFSConfiguration.Default);
item = cf.RootStorage.GetStream("AStream");
Assert.IsTrue(item != null);
Assert.IsTrue(item.Size == INITIAL_SIZE - DELTA_SIZE);

byte[] buffer = new byte[INITIAL_SIZE - DELTA_SIZE];
item.Read(buffer, 0, INITIAL_SIZE - DELTA_SIZE);
Assert.IsTrue(Helpers.CompareBuffer(b, buffer, INITIAL_SIZE - DELTA_SIZE));
cf.Close();

}

[TestMethod]
Expand Down Expand Up @@ -889,11 +904,13 @@ public void Test_RESIZE_STREAM_TRANSITION_TO_NORMAL()
}

[TestMethod]
public void Test_RESIZE_MINISTREAM_NO_TRANSITION()
public void Test_RESIZE_MINISTREAM_NO_TRANSITION_MOD()
{
CompoundFile cf = null;
int INITIAL_SIZE = 1024 * 2;
int SIZE_DELTA = 148;

byte[] b = Helpers.GetBuffer(1024 * 2);
byte[] b = Helpers.GetBuffer(INITIAL_SIZE);

cf = new CompoundFile(CFSVersion.Ver_3, CFSConfiguration.Default);
cf.RootStorage.AddStream("MiniStream").SetData(b);
Expand All @@ -902,7 +919,7 @@ public void Test_RESIZE_MINISTREAM_NO_TRANSITION()

cf = new CompoundFile("$Test_RESIZE_MINISTREAM.cfs", CFSUpdateMode.Update, CFSConfiguration.SectorRecycle | CFSConfiguration.EraseFreeSectors);
CFStream item = cf.RootStorage.GetStream("MiniStream");
item.Resize(item.Size / 2);
item.Resize(item.Size - SIZE_DELTA);

cf.Commit();
cf.Close();
Expand All @@ -911,12 +928,12 @@ public void Test_RESIZE_MINISTREAM_NO_TRANSITION()
CFStream st = cf.RootStorage.GetStream("MiniStream");

Assert.IsNotNull(st);
Assert.IsTrue(st.Size == 1024);
Assert.IsTrue(st.Size == (INITIAL_SIZE - SIZE_DELTA));

byte[] buffer = new byte[1024];
st.Read(buffer, 0, 1024);
byte[] buffer = new byte[INITIAL_SIZE - SIZE_DELTA];
st.Read(buffer, 0, INITIAL_SIZE - SIZE_DELTA);

Assert.IsTrue(Helpers.CompareBuffer(b, buffer, 1024));
Assert.IsTrue(Helpers.CompareBuffer(b, buffer, INITIAL_SIZE - SIZE_DELTA));

cf.Close();
}
Expand Down Expand Up @@ -1116,5 +1133,32 @@ public void Test_CHANGE_STREAM_NAME_FIX_54()
}
}

/// <summary>
/// Resize without transitio to smaller chain has a wrong behaviour
/// </summary>
[TestMethod]
public void TEST_RESIZE_STREAM_BUG_119()
{
var cf = new CompoundFile();
const string DATA = "data";
var st = cf.RootStorage.AddStream(DATA);
const int size = 10;
var data = Enumerable.Range(0, size).Select(v => (byte)v).ToArray();
st.SetData(data);
var ms = new MemoryStream();
cf.Save(ms);
cf.Close();

ms.Position = 0;
cf = new CompoundFile(ms, CFSUpdateMode.Update, CFSConfiguration.Default);
st = cf.RootStorage.GetStream(DATA);
byte[] buffer = new byte[size];
st.Read(buffer, 0, size);
st.Resize(5); // <- can be any number smaller than the current size
st.Write(new byte[] { 0 }, 0); // <- exception here! //NO MORE
cf.Commit();
cf.Close();
}

}
}
40 changes: 40 additions & 0 deletions sources/Test/OpenMcdf.Test/CFSTorageTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,46 @@ public void Test_FIX_BUG_31()

}

[TestMethod]
public void Test_FIX_BUG_116()
{
CompoundFile cf = new CompoundFile();
cf.RootStorage.AddStorage("AStorage")

.AddStream("AStream")
.SetData(Helpers.GetBuffer(100));

cf.SaveAs("Hello$File");

cf.Close();

CompoundFile cf1 = new CompoundFile("Hello$File", CFSUpdateMode.Update, CFSConfiguration.Default);

try
{

cf1.RootStorage.RenameItem("AStorage", "NewStorage");
cf1.Commit();
cf1.Close();

}
catch (Exception ex)
{
Assert.Fail(ex.Message);
}

try
{
CompoundFile cf2 = new CompoundFile("Hello$File");
var st2 = cf2.RootStorage.GetStorage("NewStorage");
Assert.IsNotNull(st2);
}catch(Exception ex)
{
Assert.Fail($"{ex.Message}");
}

}

[TestMethod]
[ExpectedException(typeof(OpenMcdf.CFCorruptedFileException))]
public void Test_CORRUPTEDDOC_BUG36_SHOULD_THROW_CORRUPTED_FILE_EXCEPTION()
Expand Down
1 change: 1 addition & 0 deletions sources/Test/OpenMcdf.Test/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public static byte[] GetBuffer(int count)
r.NextBytes(b);
return b;
}

public static void FillBufferWithRandomData(byte[] buffer)
{
Random r = new Random();
Expand Down

0 comments on commit d18fe49

Please sign in to comment.