Skip to content

Commit 9eab900

Browse files
committed
Merge branch 'csharp348'
2 parents 5c60890 + 5db737c commit 9eab900

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

Bson/ObjectModel/ObjectId.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
using System.Collections.Generic;
1818
using System.Diagnostics;
1919
using System.Linq;
20+
using System.Runtime.CompilerServices;
21+
using System.Security;
2022
using System.Security.Cryptography;
2123
using System.Text;
2224
using System.Threading;
@@ -48,8 +50,16 @@ public struct ObjectId : IComparable<ObjectId>, IEquatable<ObjectId>
4850
static ObjectId()
4951
{
5052
__staticMachine = GetMachineHash();
51-
__staticPid = (short)Process.GetCurrentProcess().Id; // use low order two bytes only
5253
__staticIncrement = (new Random()).Next();
54+
55+
try
56+
{
57+
__staticPid = (short)GetCurrentProcessId(); // use low order two bytes only
58+
}
59+
catch (SecurityException)
60+
{
61+
__staticPid = 0;
62+
}
5363
}
5464

5565
// constructors
@@ -351,6 +361,17 @@ public static void Unpack(byte[] bytes, out int timestamp, out int machine, out
351361
}
352362

353363
// private static methods
364+
/// <summary>
365+
/// Gets the current process id. This method exists because of how CAS operates on the call stack, checking
366+
/// for permissions before executing the method. Hence, if we inlined this call, the calling method would not execute
367+
/// before throwing an exception requiring the try/catch at an even higher level that we don't necessarily control.
368+
/// </summary>
369+
[MethodImpl(MethodImplOptions.NoInlining)]
370+
private static int GetCurrentProcessId()
371+
{
372+
return Process.GetCurrentProcess().Id;
373+
}
374+
354375
private static int GetMachineHash()
355376
{
356377
var hostName = Environment.MachineName; // use instead of Dns.HostName so it will work offline

GlobalAssemblyInfo.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616
using System.Reflection;
1717
using System.Runtime.CompilerServices;
1818
using System.Runtime.InteropServices;
19+
using System.Security;
1920

2021
[assembly: AssemblyCompany("10gen Inc.")]
2122
[assembly: AssemblyCopyright("Copyright © 2010-2012 10gen Inc.")]
2223
[assembly: AssemblyTrademark("")]
2324
[assembly: AssemblyCulture("")]
25+
[assembly: AllowPartiallyTrustedCallers]
2426

2527
// Setting ComVisible to false makes the types in this assembly not visible
2628
// to COM components. If you need to access a type in this assembly from

0 commit comments

Comments
 (0)