Skip to content

Commit 078137e

Browse files
Harden Firebase version string caches (#169)
1 parent 38c9e90 commit 078137e

3 files changed

Lines changed: 34 additions & 11 deletions

File tree

source/Firebase/Auth/Extension.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,24 @@
44
namespace Firebase.Auth
55
{
66
public partial class Auth {
7-
static string currentVersion;
7+
static string? currentVersion;
88
public static string CurrentVersion {
99
get {
1010
if (currentVersion == null) {
1111
IntPtr RTLD_MAIN_ONLY = Dlfcn.dlopen (null, 0);
12-
IntPtr ptr = Dlfcn.dlsym (RTLD_MAIN_ONLY, "FirebaseAuthVersionStr");
13-
currentVersion = Marshal.PtrToStringAnsi (ptr);
14-
Dlfcn.dlclose (RTLD_MAIN_ONLY);
12+
if (RTLD_MAIN_ONLY == IntPtr.Zero)
13+
throw new InvalidOperationException ("Unable to open the main program handle.");
14+
15+
try {
16+
IntPtr ptr = Dlfcn.dlsym (RTLD_MAIN_ONLY, "FirebaseAuthVersionStr");
17+
if (ptr == IntPtr.Zero)
18+
throw new InvalidOperationException ("Unable to resolve FirebaseAuthVersionStr.");
19+
20+
currentVersion = Marshal.PtrToStringAnsi (ptr)
21+
?? throw new InvalidOperationException ("Unable to read FirebaseAuthVersionStr.");
22+
} finally {
23+
Dlfcn.dlclose (RTLD_MAIN_ONLY);
24+
}
1525
}
1626

1727
return currentVersion;

source/Firebase/CloudFunctions/Extension.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,24 @@
33
using ObjCRuntime;
44
namespace Firebase.CloudFunctions {
55
public partial class CloudFunctions {
6-
static string currentVersion;
6+
static string? currentVersion;
77
public static string CurrentVersion {
88
get {
99
if (currentVersion == null) {
1010
IntPtr RTLD_MAIN_ONLY = Dlfcn.dlopen (null, 0);
11-
IntPtr ptr = Dlfcn.dlsym (RTLD_MAIN_ONLY, "FirebaseCloudFunctionsVersionStr");
12-
currentVersion = Marshal.PtrToStringAnsi (ptr);
13-
Dlfcn.dlclose (RTLD_MAIN_ONLY);
11+
if (RTLD_MAIN_ONLY == IntPtr.Zero)
12+
throw new InvalidOperationException ("Unable to open the main program handle.");
13+
14+
try {
15+
IntPtr ptr = Dlfcn.dlsym (RTLD_MAIN_ONLY, "FirebaseCloudFunctionsVersionStr");
16+
if (ptr == IntPtr.Zero)
17+
throw new InvalidOperationException ("Unable to resolve FirebaseCloudFunctionsVersionStr.");
18+
19+
currentVersion = Marshal.PtrToStringAnsi (ptr)
20+
?? throw new InvalidOperationException ("Unable to read FirebaseCloudFunctionsVersionStr.");
21+
} finally {
22+
Dlfcn.dlclose (RTLD_MAIN_ONLY);
23+
}
1424
}
1525

1626
return currentVersion;

source/Firebase/Core/Extension.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@ public partial class App {
88
[DllImport ("__Internal", EntryPoint = "FIRFirebaseVersion")]
99
extern internal static IntPtr _FIRFirebaseVersion ();
1010

11-
static string firebaseVersion;
11+
static string? firebaseVersion;
1212
public static string FirebaseVersion {
1313
get {
1414
if (firebaseVersion == null) {
15-
1615
IntPtr verStrPtr = _FIRFirebaseVersion ();
17-
firebaseVersion = NSString.FromHandle (verStrPtr);
16+
if (verStrPtr == IntPtr.Zero)
17+
throw new InvalidOperationException ("Unable to resolve FIRFirebaseVersion.");
18+
19+
firebaseVersion = NSString.FromHandle (verStrPtr)
20+
?? throw new InvalidOperationException ("Unable to read FIRFirebaseVersion.");
1821
}
1922

2023
return firebaseVersion;

0 commit comments

Comments
 (0)