Эх сурвалжийг харах

Dynamic memory allocation of device name (closes #11)

Stéphane Raimbault 12 жил өмнө
parent
commit
541804b30e

+ 1 - 0
src/modbus-private.h

@@ -111,6 +111,7 @@ typedef struct _modbus_backend {
     void (*close) (modbus_t *ctx);
     int (*flush) (modbus_t *ctx);
     int (*select) (modbus_t *ctx, fd_set *rset, struct timeval *tv, int msg_length);
+    void (*free) (modbus_t *ctx);
 } modbus_backend_t;
 
 struct _modbus {

+ 2 - 10
src/modbus-rtu-private.h

@@ -59,16 +59,8 @@ struct win32_ser {
 #endif /* _WIN32 */
 
 typedef struct _modbus_rtu {
-    /* Device: "/dev/ttyS0", "/dev/ttyUSB0" or "/dev/tty.USA19*" on Mac OS X for
-       KeySpan USB<->Serial adapters this string had to be made bigger on OS X
-       as the directory+file name was bigger than 19 bytes. Making it 67 bytes
-       for now, but OS X does support 256 byte file names. May become a problem
-       in the future. */
-#if defined(__APPLE_CC__)
-    char device[64];
-#else
-    char device[16];
-#endif
+    /* Device: "/dev/ttyS0", "/dev/ttyUSB0" or "/dev/tty.USA19*" on Mac OS X. */
+    char *device;
     /* Bauds: 9600, 19200, 57600, 115200, etc */
     int baud;
     /* Data bit */

+ 13 - 12
src/modbus-rtu.c

@@ -987,6 +987,12 @@ int _modbus_rtu_select(modbus_t *ctx, fd_set *rset,
     return s_rc;
 }
 
+void _modbus_rtu_free(modbus_t *ctx) {
+    free(((modbus_rtu_t*)ctx->backend_data)->device);
+    free(ctx->backend_data);
+    free(ctx);
+}
+
 const modbus_backend_t _modbus_rtu_backend = {
     _MODBUS_BACKEND_TYPE_RTU,
     _MODBUS_RTU_HEADER_LENGTH,
@@ -1005,7 +1011,8 @@ const modbus_backend_t _modbus_rtu_backend = {
     _modbus_rtu_connect,
     _modbus_rtu_close,
     _modbus_rtu_flush,
-    _modbus_rtu_select
+    _modbus_rtu_select,
+    _modbus_rtu_free
 };
 
 modbus_t* modbus_new_rtu(const char *device,
@@ -1014,8 +1021,7 @@ modbus_t* modbus_new_rtu(const char *device,
 {
     modbus_t *ctx;
     modbus_rtu_t *ctx_rtu;
-    size_t dest_size;
-    size_t ret_size;
+    size_t device_size;
 
     ctx = (modbus_t *) malloc(sizeof(modbus_t));
     _modbus_init_common(ctx);
@@ -1024,21 +1030,16 @@ modbus_t* modbus_new_rtu(const char *device,
     ctx->backend_data = (modbus_rtu_t *) malloc(sizeof(modbus_rtu_t));
     ctx_rtu = (modbus_rtu_t *)ctx->backend_data;
 
-    dest_size = sizeof(ctx_rtu->device);
-    ret_size = strlcpy(ctx_rtu->device, device, dest_size);
-    if (ret_size == 0) {
+    device_size = sizeof(device);
+    if (device_size == 0) {
         fprintf(stderr, "The device string is empty\n");
         modbus_free(ctx);
         errno = EINVAL;
         return NULL;
     }
 
-    if (ret_size >= dest_size) {
-        fprintf(stderr, "The device string has been truncated\n");
-        modbus_free(ctx);
-        errno = EINVAL;
-        return NULL;
-    }
+    ctx_rtu->device = (char *) malloc(device_size);
+    strcpy(ctx_rtu->device, device);
 
     ctx_rtu->baud = baud;
     if (parity == 'N' || parity == 'E' || parity == 'O') {

+ 9 - 2
src/modbus-tcp.c

@@ -654,6 +654,11 @@ int _modbus_tcp_select(modbus_t *ctx, fd_set *rset, struct timeval *tv, int leng
     return s_rc;
 }
 
+void _modbus_tcp_free(modbus_t *ctx) {
+    free(ctx->backend_data);
+    free(ctx);
+}
+
 const modbus_backend_t _modbus_tcp_backend = {
     _MODBUS_BACKEND_TYPE_TCP,
     _MODBUS_TCP_HEADER_LENGTH,
@@ -672,7 +677,8 @@ const modbus_backend_t _modbus_tcp_backend = {
     _modbus_tcp_connect,
     _modbus_tcp_close,
     _modbus_tcp_flush,
-    _modbus_tcp_select
+    _modbus_tcp_select,
+    _modbus_tcp_free
 };
 
 
@@ -694,7 +700,8 @@ const modbus_backend_t _modbus_tcp_pi_backend = {
     _modbus_tcp_pi_connect,
     _modbus_tcp_close,
     _modbus_tcp_flush,
-    _modbus_tcp_select
+    _modbus_tcp_select,
+    _modbus_tcp_free
 };
 
 modbus_t* modbus_new_tcp(const char *ip, int port)

+ 1 - 2
src/modbus.c

@@ -1462,8 +1462,7 @@ void modbus_free(modbus_t *ctx)
     if (ctx == NULL)
         return;
 
-    free(ctx->backend_data);
-    free(ctx);
+    ctx->backend->free(ctx);
 }
 
 void modbus_set_debug(modbus_t *ctx, int boolean)