fix(rtable): Potential memory leak in rte replace
[ric-plt/lib/rmr.git] / src / common / src / rtc_static.c
index 6b9c707..013dc8d 100644 (file)
@@ -8,7 +8,7 @@
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
 
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
 
-       http://www.apache.org/licenses/LICENSE-2.0
+          http://www.apache.org/licenses/LICENSE-2.0
 
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
 
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
@@ -84,7 +84,7 @@ static void* rtc( void* vctx ) {
        char*   port;                                           // a port number we listen/connect to
        char*   fport;                                          // pointer to the real buffer to free
        size_t  buf_size;                                       // nng needs var pointer not just size?
        char*   port;                                           // a port number we listen/connect to
        char*   fport;                                          // pointer to the real buffer to free
        size_t  buf_size;                                       // nng needs var pointer not just size?
-       char*   nextr;                                          // pointer at next record in the message        
+       char*   nextr;                                          // pointer at next record in the message
        char*   curr;                                           // current record
        int     i;
        long    blabber = 0;                            // time of last blabber so we don't flood if rtg goes bad
        char*   curr;                                           // current record
        int     i;
        long    blabber = 0;                            // time of last blabber so we don't flood if rtg goes bad
@@ -92,7 +92,7 @@ static void* rtc( void* vctx ) {
        int             state;                                          // processing state of some nng function
        char*   tokens[128];
        char    wbuf[128];
        int             state;                                          // processing state of some nng function
        char*   tokens[128];
        char    wbuf[128];
-       char*   pbuf;
+       char*   pbuf = NULL;
        int             pbuf_size = 0;                          // number allocated in pbuf
        int             ntoks;
        int             raw_interface = 1;                      // rtg is using raw NNG/Nano not RMr to send updates
        int             pbuf_size = 0;                          // number allocated in pbuf
        int             ntoks;
        int             raw_interface = 1;                      // rtg is using raw NNG/Nano not RMr to send updates
@@ -106,7 +106,7 @@ static void* rtc( void* vctx ) {
        }
 
        if( (eptr = getenv( ENV_VERBOSE_FILE )) != NULL ) {
        }
 
        if( (eptr = getenv( ENV_VERBOSE_FILE )) != NULL ) {
-               vfd = open( eptr, O_RDONLY ); 
+               vfd = open( eptr, O_RDONLY );
                if( vfd >= 0 ) {
                        wbuf[0] = 0;
                        lseek( vfd, 0, 0 );
                if( vfd >= 0 ) {
                        wbuf[0] = 0;
                        lseek( vfd, 0, 0 );
@@ -131,7 +131,7 @@ static void* rtc( void* vctx ) {
 
        ntoks = uta_tokenise( port, tokens, 120, ':' );                 // assume tcp:port, but it could be port or old style host:port
        switch( ntoks ) {
 
        ntoks = uta_tokenise( port, tokens, 120, ':' );                 // assume tcp:port, but it could be port or old style host:port
        switch( ntoks ) {
-               case 1: 
+               case 1:
                                port = tokens[0];                       // just the port
                                break;
 
                                port = tokens[0];                       // just the port
                                break;
 
@@ -183,8 +183,12 @@ static void* rtc( void* vctx ) {
                                if( pbuf ) {
                                        free( pbuf );
                                }
                                if( pbuf ) {
                                        free( pbuf );
                                }
-                               pbuf = (char *) malloc( sizeof( char ) * mlen *2 );
-                               pbuf_size = mlen * 2;
+                               if( mlen < 512 ) {
+                                       pbuf_size = 512;
+                               } else {
+                                       pbuf_size = mlen * 2;
+                               }
+                               pbuf = (char *) malloc( sizeof( char ) * pbuf_size );
                        }
                        memcpy( pbuf, payload, mlen );
                        pbuf[mlen] = 0;                                                                         // don't depend on sender making this a legit string
                        }
                        memcpy( pbuf, payload, mlen );
                        pbuf[mlen] = 0;                                                                         // don't depend on sender making this a legit string
@@ -192,19 +196,19 @@ static void* rtc( void* vctx ) {
                        curr = pbuf;
                        while( curr ) {                                                         // loop over each record in the buffer
                                nextr = strchr( curr, '\n' );                   // allow multiple newline records, find end of current and mark
                        curr = pbuf;
                        while( curr ) {                                                         // loop over each record in the buffer
                                nextr = strchr( curr, '\n' );                   // allow multiple newline records, find end of current and mark
-       
+
                                if( nextr ) {
                                        *(nextr++) = 0;
                                }
                                if( nextr ) {
                                        *(nextr++) = 0;
                                }
-       
+
                                if( vlevel > 1 ) {
                                        fprintf( stderr, "[DBUG] rmr_rtc: processing (%s)\n", curr );
                                }
                                parse_rt_rec( ctx, curr, vlevel );              // parse record and add to in progress table
                                if( vlevel > 1 ) {
                                        fprintf( stderr, "[DBUG] rmr_rtc: processing (%s)\n", curr );
                                }
                                parse_rt_rec( ctx, curr, vlevel );              // parse record and add to in progress table
-               
+
                                curr = nextr;
                        }
                                curr = nextr;
                        }
-       
+
                        if( ctx->shutdown ) {           // mostly for testing, but allows user app to close us down if rmr_*() function sets this
                                break;
                        }
                        if( ctx->shutdown ) {           // mostly for testing, but allows user app to close us down if rmr_*() function sets this
                                break;
                        }