剛看了一篇文章,講在java世界裡析構方法可能引起的漏洞以及如何防範。
馬上在C#中試了一下,發現.net也有同樣的問題。代碼如下
1 using System;
2 using System.Threading;
3
4 namespace TestProj
5 {
6
7
8 class Vulnerable
9 {
10 int value = 0;
11
12 public Vulnerable(int value)
13 {
14 if (value <= 0)
15 {
16 throw new ArgumentException("Vulnerable value must be positive");
17 }
18 this.value = value;
19 }
20 public override string ToString()
21 {
22 return (value.ToString());
23 }
24 }
25
26 class AttackVulnerable : Vulnerable
27 {
28 static Vulnerable vulnerable;
29
30 public AttackVulnerable(int value)
31 : base(value)
32 {
33 }
34
35 ~AttackVulnerable()
36 {
37 vulnerable = this;
38 }
39
40 public static void Main(string[] args)
41 {
42 try
43 {
44 new AttackVulnerable(-1);
45 }
46 catch (Exception e)
47 {
48 Console.WriteLine(e.Message);
49 }
50 GC.Collect();
51 Thread.Sleep(1000);//wait for gc to complete
52 if (vulnerable != null)
53 {
54 Console.WriteLine("Vulnerable object " + vulnerable + " created!");
55 }
56 Console.ReadLine();
57 }
58 }
59 }
運行結果如下
很明顯結果是漏洞存在。
關鍵是原文中給出的java世界的解決辦法不知在C#中有無對應的實現?真心求教部落格園各位高人。
java世界中可以在調用自身構造器和調用object構造器(其實是基類構造器啦)中間執行一段static代碼感覺挺牛的。不知道java中的那個Void怎麼在c#中實現呢?
[Update].net中可以這樣實現
1 using System;
2 using System.Threading;
3
4 namespace TestProj
5 {
6 class Invulnerable
7 {
8 int value = 0;
9
10 public Invulnerable(int value)
11 {
12 GC.SuppressFinalize(this);
13 if (value <= 0)
14 {
15 throw new ArgumentException("Invulnerable value must be positive");
16 }
17 this.value = value;
18 }
19
20 public override string ToString()
21 {
22 return (value.ToString());
23 }
24 }
25
26 class AttackInvulnerable : Invulnerable
27 {
28 static Invulnerable vulnerable;
29
30 public AttackInvulnerable(int value)
31 : base(value)
32 {
33 }
34
35 ~AttackInvulnerable()
36 {
37 vulnerable = this;
38 }
39
40 public static void Main(string[] args)
41 {
42 try
43 {
44 new AttackInvulnerable(-1);
45 }
46 catch (Exception e)
47 {
48 Console.WriteLine(e.Message);
49 }
50
51 GC.Collect();
52 Thread.Sleep(1000);//wait for gc to complete
53 if (vulnerable != null)
54 {
55 Console.WriteLine("Vulnerable object " + vulnerable + " created!");
56 }
57 else
58 {
59 Console.WriteLine("Attack failed");
60 }
61 Console.ReadLine();
62 }
63 }
64 }