sandbox / mbed-client

Fork of mbed-client by Christopher Haster

Revision:
4:ae5178938864
Parent:
1:79b6cc67d8b4
--- a/source/m2msecurity.cpp	Fri Feb 19 17:44:50 2016 +0000
+++ b/source/m2msecurity.cpp	Sat Apr 02 00:31:13 2016 +0300
@@ -19,7 +19,11 @@
 #include "mbed-client/m2mobject.h"
 #include "mbed-client/m2mobjectinstance.h"
 #include "mbed-client/m2mresource.h"
+#include "mbed-client/m2mstring.h"
 
+#define TRACE_GROUP "mClt"
+
+#define BUFFER_SIZE 21
 
 M2MSecurity::M2MSecurity(ServerType ser_type)
 : M2MObject(M2M_SECURITY_ID),
@@ -119,12 +123,13 @@
                                                             false);
 
             if(res) {
-                char *buffer = (char*)malloc(20);
+                res->set_operation(M2MBase::NOT_ALLOWED);
+                char *buffer = (char*)malloc(BUFFER_SIZE);
                 if(buffer) {
-                    int size = snprintf(buffer, 20,"%ld",(long int)value);
-                    res->set_operation(M2MBase::NOT_ALLOWED);
-                    res->set_value((const uint8_t*)buffer,
-                                   (uint32_t)size);
+                    uint32_t size = m2m::itoa_c(value, buffer);
+                    if (size <= BUFFER_SIZE) {
+                        res->set_value((const uint8_t*)buffer, size);
+                    }
                     free(buffer);
                 }
             }
@@ -189,12 +194,13 @@
            M2MSecurity::ShortServerID == resource       ||
            M2MSecurity::ClientHoldOffTime == resource) {
             // If it is any of the above resource
-            // set the value of the resource.
-            char *buffer = (char*)malloc(20);
+            // set the value of the resource.            
+            char *buffer = (char*)malloc(BUFFER_SIZE);
             if(buffer) {
-                int size = snprintf(buffer, 20,"%ld",(long int)value);
-                success = res->set_value((const uint8_t*)buffer,
-                                         (uint32_t)size);
+                uint32_t size = m2m::itoa_c(value, buffer);
+                if (size <= BUFFER_SIZE) {
+                    success = res->set_value((const uint8_t*)buffer, size);
+                }
                 free(buffer);
             }
         }