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

Throw exception on serialize 32bit time older than unix epoch #118

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ namespace UniJSON
public static class DateTimeOffsetExtensions
{
public const long TicksPerSecond = 10000000;
public readonly static DateTimeOffset EpocTime = new DateTimeOffset(1970, 1, 1, 0, 0, 0, 0, TimeSpan.Zero);
public readonly static DateTimeOffset EpochTime = new DateTimeOffset(1970, 1, 1, 0, 0, 0, 0, TimeSpan.Zero);
[Obsolete("Use EpochTime")]
public readonly static DateTimeOffset EpocTime = EpochTime;
#if !NET_4_6 && !NET_STANDARD_2_0
public static long ToUnixTimeSeconds(this DateTimeOffset now)
{
if (now < EpocTime)
if (now < EpochTime)
{
throw new ArgumentOutOfRangeException();
}
return (now - EpocTime).Ticks / TicksPerSecond;
return (now - EpochTime).Ticks / TicksPerSecond;
}
#endif
}
Expand Down
5 changes: 5 additions & 0 deletions Assets/VRM/UniJSON/Scripts/MsgPack/MsgPackFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,11 @@ public void Value(ArraySegment<byte> bytes)

public void TimeStamp32(DateTimeOffset time)
{
// https://github.com/ousttrue/UniJSON/blob/1.2/Scripts/Extensions/DateTimeOffsetExtensions.cs#L13-L16
if (time < DateTimeOffsetExtensions.EpochTime)
{
throw new ArgumentOutOfRangeException();
}
m_store.Write((Byte)MsgPackType.FIX_EXT_4);
m_store.Write((SByte)(-1));
m_store.WriteBigEndian((uint)time.ToUnixTimeSeconds());
Expand Down
2 changes: 1 addition & 1 deletion Assets/VRM/UniJSON/Scripts/MsgPack/MsgPackValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ public T GetValue<T>()
if (GetExtType() == -1)
{
var unixtime = EndianConverter.NetworkByteDWordToUnsignedNativeByteOrder(GetBody());
var dt = new DateTimeOffset(unixtime * DateTimeOffsetExtensions.TicksPerSecond + DateTimeOffsetExtensions.EpocTime.Ticks, TimeSpan.Zero);
var dt = new DateTimeOffset(unixtime * DateTimeOffsetExtensions.TicksPerSecond + DateTimeOffsetExtensions.EpochTime.Ticks, TimeSpan.Zero);
return GenericCast<DateTimeOffset, T>.Cast(dt);
}
break;
Expand Down