Procházet zdrojové kódy

intoduces mallocing in the generated decode functions as proposed in issue #20
closes issue #24
@FlorianPalm : plase review the //FIXME mark in the secureChannelLayer.c

Stasik0 před 11 roky
rodič
revize
b4dc6e4ccf
2 změnil soubory, kde provedl 11 přidání a 4 odebrání
  1. 1 0
      src/opcua_secureChannelLayer.c
  2. 10 4
      tool/generate_builtin.py

+ 1 - 0
src/opcua_secureChannelLayer.c

@@ -353,6 +353,7 @@ UA_Int32 SL_processMessage(UA_connection *connection, UA_ByteString message) {
 		UA_RequestHeader requestHeader;
 		UA_ExtensionObject additionalHeader;
 		//FIXME: Sten the structure was empty and decode attempted to fill it -> segfault
+		//FIXME: Sten: probably not needed anymore, since memory gets malloced in the decoder
 		requestHeader.additionalHeader = &additionalHeader;
 		// 	Req-2) UInt32 ClientProtocolVersion
 		UA_UInt32 clientProtocolVersion;

+ 10 - 4
tool/generate_builtin.py

@@ -152,7 +152,9 @@ def createStructured(element):
 		print("UA_Int32 "  + name + "_calcSize(" + name + " const * ptr) {\n\treturn 0", end='', file=fc) 
     else:
 	# code 
-        print("UA_Int32 "  + name + "_calcSize(" + name + " const * ptr) {\n\treturn 0", end='', file=fc)
+		print("UA_Int32 "  + name + "_calcSize(" + name + " const * ptr) {", end='', file=fc)
+		print("\n\tif(ptr==UA_NULL){return sizeof("+ name +");}", end='', file=fc)
+		print("\n\treturn 0", end='', file=fc)
 
     # code _calcSize
     for n,t in valuemap.iteritems():
@@ -197,10 +199,14 @@ def createStructured(element):
             if t in enum_types:
                 print('\tretval |= UA_'+t+'_decode(src,pos,&(dst->'+n+'));', end='\n', file=fc)
             elif t.find("**") != -1:
-                print('\tretval |= UA_Int32_decode(src,pos,&(dst->'+n+'Size)); // decode size', end='\n', file=fc)
-		print("\tretval |= UA_Array_decode(src,dst->"+n+"Size, UA_" + t[0:t.find("*")].upper()+",pos,(void const**) (dst->"+n+"));", end='\n', file=fc) #not tested
+				print('\tretval |= UA_Int32_decode(src,pos,&(dst->'+n+'Size)); // decode size', end='\n', file=fc)
+				#allocate memory
+				print('\tretval |= UA_alloc((void**)&(dst->' + n + "),dst->" + n + "Size*sizeof(void*));", end='\n', file=fc)
+				print("\tretval |= UA_Array_decode(src,dst->"+n+"Size, UA_" + t[0:t.find("*")].upper()+",pos,(void const**) (dst->"+n+"));", end='\n', file=fc) #not tested
             elif t.find("*") != -1:
-                print('\tretval |= UA_' + t[0:t.find("*")] + "_decode(src,pos,dst->"+ n +");", end='\n', file=fc)
+				#allocate memory
+				print('\tretval |= UA_alloc((void**)&(dst->' + n + "),UA_" + t[0:t.find("*")] +"_calcSize(UA_NULL));", end='\n', file=fc)
+				print('\tretval |= UA_' + t[0:t.find("*")] + "_decode(src,pos,dst->"+ n +");", end='\n', file=fc)
             else:
                 print('\tretval |= UA_'+t+"_decode(src,pos,&(dst->"+n+"));", end='\n', file=fc)
     print("\treturn retval;\n}\n", end='\n', file=fc)