comparison src/luan/impl/Closure.java @ 1133:ba4daf107e07

fix security bug
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 17 Jan 2018 20:59:42 -0700
parents c49980cdece6
children 3ef883468fd0
comparison
equal deleted inserted replaced
1132:b70102bab110 1133:ba4daf107e07
4 import luan.LuanFunction; 4 import luan.LuanFunction;
5 import luan.LuanState; 5 import luan.LuanState;
6 import luan.LuanException; 6 import luan.LuanException;
7 import luan.LuanCloner; 7 import luan.LuanCloner;
8 import luan.LuanCloneable; 8 import luan.LuanCloneable;
9 import luan.LuanJava; 9 import luan.LuanJavaOk;
10 10
11 11
12 public abstract class Closure extends LuanFunction implements LuanCloneable, Cloneable { 12 public abstract class Closure extends LuanFunction implements LuanCloneable, Cloneable {
13 public Pointer[] upValues; 13 public Pointer[] upValues;
14 public LuanJava ljava; 14 public LuanJavaOk javaOk;
15 private LuanCloner cloner; 15 private LuanCloner cloner;
16 16
17 public Closure(int nUpValues,LuanJava java) throws LuanException { 17 public Closure(int nUpValues,LuanJavaOk javaOk) throws LuanException {
18 this.upValues = new Pointer[nUpValues]; 18 this.upValues = new Pointer[nUpValues];
19 this.ljava = java; 19 this.javaOk = javaOk;
20 } 20 }
21 21
22 @Override public Closure shallowClone() { 22 @Override public Closure shallowClone() {
23 try { 23 try {
24 return (Closure)clone(); 24 return (Closure)clone();
31 check(); 31 check();
32 Closure clone = (Closure)dc; 32 Closure clone = (Closure)dc;
33 switch( cloner.type ) { 33 switch( cloner.type ) {
34 case COMPLETE: 34 case COMPLETE:
35 clone.upValues = (Pointer[])cloner.clone(upValues); 35 clone.upValues = (Pointer[])cloner.clone(upValues);
36 clone.ljava = (LuanJava)cloner.clone(ljava); 36 clone.javaOk = (LuanJavaOk)cloner.clone(javaOk);
37 return; 37 return;
38 case INCREMENTAL: 38 case INCREMENTAL:
39 clone.cloner = cloner; 39 clone.cloner = cloner;
40 clone.upValues = upValues; 40 clone.upValues = upValues;
41 clone.ljava = ljava; 41 clone.javaOk = javaOk;
42 return; 42 return;
43 } 43 }
44 } 44 }
45 45
46 private void check() { 46 private void check() {
47 if( cloner != null ) { 47 if( cloner != null ) {
48 upValues = (Pointer[])cloner.clone(upValues); 48 upValues = (Pointer[])cloner.clone(upValues);
49 ljava = (LuanJava)cloner.clone(ljava); 49 javaOk = (LuanJavaOk)cloner.clone(javaOk);
50 cloner = null; 50 cloner = null;
51 } 51 }
52 } 52 }
53 53
54 @Override public final Object call(LuanState luan,Object[] args) throws LuanException { 54 @Override public final Object call(LuanState luan,Object[] args) throws LuanException {
55 if( luan.isLocked ) 55 if( luan.isLocked )
56 throw new RuntimeException("luan is locked"); 56 throw new RuntimeException("luan is locked");
57 check(); 57 check();
58 LuanJava old = luan.java; 58 LuanJavaOk old = luan.javaOk;
59 luan.java = ljava; 59 luan.javaOk = javaOk;
60 try { 60 try {
61 return doCall(luan,args); 61 return doCall(luan,args);
62 } catch(StackOverflowError e) { 62 } catch(StackOverflowError e) {
63 throw new LuanException( "stack overflow" ); 63 throw new LuanException( "stack overflow" );
64 } finally { 64 } finally {
65 luan.java = old; 65 luan.javaOk = old;
66 } 66 }
67 } 67 }
68 68
69 public abstract Object doCall(LuanState luan,Object[] args) throws LuanException; 69 public abstract Object doCall(LuanState luan,Object[] args) throws LuanException;
70 } 70 }