Trial updated library for RPCInterface. The original library by Michael Walker now includes this fix. The trial library here will be deleted shortly...

Dependents:   HTTPServerExample_LR WebServerRPC WebServerRPC_Mai19 12_06_22_correction_probleme

Committer:
hexley
Date:
Fri Feb 04 00:56:43 2011 +0000
Revision:
0:1c61049a0349
Memory leak addressed in RPCFunction.cpp

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hexley 0:1c61049a0349 1 /**
hexley 0:1c61049a0349 2 * @section LICENSE
hexley 0:1c61049a0349 3 *Copyright (c) 2010 ARM Ltd.
hexley 0:1c61049a0349 4 *
hexley 0:1c61049a0349 5 *Permission is hereby granted, free of charge, to any person obtaining a copy
hexley 0:1c61049a0349 6 *of this software and associated documentation files (the "Software"), to deal
hexley 0:1c61049a0349 7 *in the Software without restriction, including without limitation the rights
hexley 0:1c61049a0349 8 *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
hexley 0:1c61049a0349 9 *copies of the Software, and to permit persons to whom the Software is
hexley 0:1c61049a0349 10 *furnished to do so, subject to the following conditions:
hexley 0:1c61049a0349 11 *
hexley 0:1c61049a0349 12 *The above copyright notice and this permission notice shall be included in
hexley 0:1c61049a0349 13 *all copies or substantial portions of the Software.
hexley 0:1c61049a0349 14 *
hexley 0:1c61049a0349 15 *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
hexley 0:1c61049a0349 16 *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
hexley 0:1c61049a0349 17 *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
hexley 0:1c61049a0349 18 *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
hexley 0:1c61049a0349 19 *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
hexley 0:1c61049a0349 20 *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
hexley 0:1c61049a0349 21 *THE SOFTWARE.
hexley 0:1c61049a0349 22 *
hexley 0:1c61049a0349 23 * @section Description
hexley 0:1c61049a0349 24 *This class provides an object which can be called over RPC to run the function which is attached to it.
hexley 0:1c61049a0349 25 *
hexley 0:1c61049a0349 26 * Modified to address a memory leak issue at line 85. 3 Feb 2011 -hb
hexley 0:1c61049a0349 27 */
hexley 0:1c61049a0349 28 #include "RPCFunction.h"
hexley 0:1c61049a0349 29 #include "rpc.h"
hexley 0:1c61049a0349 30
hexley 0:1c61049a0349 31 //Parse a char argument without delimiting by anything that is non alphanumeric - based on version in rpc.h line 153
hexley 0:1c61049a0349 32 char *parse_arg_char(const char *arg, const char **next) {
hexley 0:1c61049a0349 33 const char *ptr = arg;
hexley 0:1c61049a0349 34 char *res = NULL;
hexley 0:1c61049a0349 35 if(*arg == '"') {
hexley 0:1c61049a0349 36 /* quoted string */
hexley 0:1c61049a0349 37 ptr = ++arg;
hexley 0:1c61049a0349 38 int len = 0;
hexley 0:1c61049a0349 39 /* find the end (and length) of the quoted string */
hexley 0:1c61049a0349 40 for(char c = *ptr; c != 0 && c != '"'; c = *++ptr) {
hexley 0:1c61049a0349 41 len++;
hexley 0:1c61049a0349 42 if(c == '\\') {
hexley 0:1c61049a0349 43 ptr++;
hexley 0:1c61049a0349 44 }
hexley 0:1c61049a0349 45 }
hexley 0:1c61049a0349 46 /* copy the quoted string, and unescape characters */
hexley 0:1c61049a0349 47 if(len != 0) {
hexley 0:1c61049a0349 48 res = new char[len+1];
hexley 0:1c61049a0349 49 char *resptr = res;
hexley 0:1c61049a0349 50 while(arg != ptr) {
hexley 0:1c61049a0349 51 *resptr++ = parse_char(arg, &arg);
hexley 0:1c61049a0349 52 }
hexley 0:1c61049a0349 53 *resptr = 0;
hexley 0:1c61049a0349 54 }
hexley 0:1c61049a0349 55 } else {
hexley 0:1c61049a0349 56 /* unquoted string */
hexley 0:1c61049a0349 57 while(isalnum(*ptr) || isgraph(*ptr) || *ptr=='_' || *ptr == ' ') { //Edit this line to change which types of characters are allowed and which delimit
hexley 0:1c61049a0349 58 ptr++;
hexley 0:1c61049a0349 59 }
hexley 0:1c61049a0349 60 int len = ptr-arg;
hexley 0:1c61049a0349 61 if(len!=0) { //Chnages made to just pass whole string with no next arg or delimiters, these changes just removes space at the beginning
hexley 0:1c61049a0349 62 res = new char[len]; //was len+1
hexley 0:1c61049a0349 63 memcpy(res, arg + 1, len - 1); // was arg, len
hexley 0:1c61049a0349 64 res[len-1] = 0; //was len
hexley 0:1c61049a0349 65 }
hexley 0:1c61049a0349 66 }
hexley 0:1c61049a0349 67
hexley 0:1c61049a0349 68 if(next != NULL) {
hexley 0:1c61049a0349 69 *next = ptr;
hexley 0:1c61049a0349 70 }
hexley 0:1c61049a0349 71 return res;
hexley 0:1c61049a0349 72 }
hexley 0:1c61049a0349 73
hexley 0:1c61049a0349 74 //Custom rpc method caller for execute so that the string will not be delimited by anything
hexley 0:1c61049a0349 75 //See line 436 of rpc.h
hexley 0:1c61049a0349 76 void rpc_method_caller_run(Base *this_ptr, const char *arguments, char *result) {
hexley 0:1c61049a0349 77
hexley 0:1c61049a0349 78 const char *next = arguments;
hexley 0:1c61049a0349 79 char* arg1 = parse_arg_char(next,NULL);
hexley 0:1c61049a0349 80
hexley 0:1c61049a0349 81 char * res = (static_cast<RPCFunction*>(this_ptr)->run)(arg1);
hexley 0:1c61049a0349 82 if(result != NULL) {
hexley 0:1c61049a0349 83 write_result<char*>(res, result);
hexley 0:1c61049a0349 84 }
hexley 0:1c61049a0349 85 delete arg1; // Seems to stop a memory leak issue
hexley 0:1c61049a0349 86 }
hexley 0:1c61049a0349 87
hexley 0:1c61049a0349 88 RPCFunction::RPCFunction(void(*f)(char*, char*), const char* name) : Base(name){
hexley 0:1c61049a0349 89 _ftr = f;
hexley 0:1c61049a0349 90 }
hexley 0:1c61049a0349 91
hexley 0:1c61049a0349 92
hexley 0:1c61049a0349 93 //Just run the attached function using the string thats in private memory - or just using null values,
hexley 0:1c61049a0349 94 char * RPCFunction::run(char * input){
hexley 0:1c61049a0349 95 strcpy(_input, input);
hexley 0:1c61049a0349 96 (*_ftr)(_input,_output);
hexley 0:1c61049a0349 97 return(_output);
hexley 0:1c61049a0349 98 }
hexley 0:1c61049a0349 99
hexley 0:1c61049a0349 100 //Just read the output string
hexley 0:1c61049a0349 101 char* RPCFunction::read(){
hexley 0:1c61049a0349 102 return(_output);
hexley 0:1c61049a0349 103 }
hexley 0:1c61049a0349 104
hexley 0:1c61049a0349 105
hexley 0:1c61049a0349 106 #ifdef MBED_RPC
hexley 0:1c61049a0349 107 const rpc_method *RPCFunction::get_rpc_methods() {
hexley 0:1c61049a0349 108 static const rpc_method rpc_methods[] = {
hexley 0:1c61049a0349 109 { "run", rpc_method_caller_run }, //Run using custom caller, all characters accepted in string
hexley 0:1c61049a0349 110 { "read", rpc_method_caller<char*, RPCFunction, &RPCFunction::read> },
hexley 0:1c61049a0349 111 RPC_METHOD_SUPER(Base)
hexley 0:1c61049a0349 112 };
hexley 0:1c61049a0349 113 return rpc_methods;
hexley 0:1c61049a0349 114 }
hexley 0:1c61049a0349 115
hexley 0:1c61049a0349 116 #endif