]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
modbus: make subfunction uint16_t
authorShivani Bhardwaj <shivanib134@gmail.com>
Wed, 8 Jul 2020 07:10:46 +0000 (12:40 +0530)
committerShivani Bhardwaj <shivanib134@gmail.com>
Wed, 8 Jul 2020 07:38:23 +0000 (13:08 +0530)
src/detect-engine-modbus.c
src/detect-modbus.c
src/detect-modbus.h

index dc6dbe9bc08e88704967ef8d6eb4f8505b4e99d2..7db6dd57911b181976d7c514d614ddb31fdda337 100644 (file)
@@ -230,10 +230,10 @@ int DetectEngineInspectModbus(ThreadVars            *tv,
         if (modbus->category == MODBUS_CAT_NONE) {
             if (modbus->function != MODBUS_FUNC_NONE) {
                 if (modbus->function == tx->function) {
-                    if (modbus->subfunction != NULL) {
+                    if (modbus->has_subfunction) {
                         SCLogDebug("looking for Modbus server function %d and subfunction %d",
-                                   modbus->function, *(modbus->subfunction));
-                        ret = (*(modbus->subfunction) == (tx->subFunction))? 1 : 0;
+                                   modbus->function, modbus->subfunction);
+                        ret = (modbus->subfunction == (tx->subFunction))? 1 : 0;
                     } else {
                         SCLogDebug("looking for Modbus server function %d", modbus->function);
                         ret = 1;
index 24075cd8f3a542215b641971ef9b54853e2a7d89..f064d67308cdaad38ce411812ac6092bf6103063 100644 (file)
@@ -91,9 +91,6 @@ static void DetectModbusFree(DetectEngineCtx *de_ctx, void *ptr) {
     DetectModbus *modbus = (DetectModbus *) ptr;
 
     if(modbus) {
-        if (modbus->subfunction)
-            SCFree(modbus->subfunction);
-
         if (modbus->unit_id)
             SCFree(modbus->unit_id);
 
@@ -355,18 +352,14 @@ static DetectModbus *DetectModbusFunctionParse(DetectEngineCtx *de_ctx, const ch
                 goto error;
             }
 
-            /* We have a correct address option */
-            modbus->subfunction =(uint16_t *) SCCalloc(1, sizeof(uint16_t));
-            if (modbus->subfunction == NULL)
-                goto error;
-
-            if (StringParseUint16(&(*modbus->subfunction), 10, 0, (const char *)arg) < 0) {
+            if (StringParseUint16(&modbus->subfunction, 10, 0, (const char *)arg) < 0) {
                 SCLogError(SC_ERR_INVALID_VALUE, "Invalid value for "
                            "modbus subfunction: %s", (const char*)arg);
                 goto error;
             }
+            modbus->has_subfunction = true;
 
-            SCLogDebug("and subfunction %d", *(modbus->subfunction));
+            SCLogDebug("and subfunction %d", modbus->subfunction);
         }
     } else {
         uint8_t neg = 0;
@@ -627,7 +620,7 @@ static int DetectModbusTest02(void)
     modbus = (DetectModbus *) de_ctx->sig_list->sm_lists_tail[g_modbus_buffer_id]->ctx;
 
     FAIL_IF_NOT(modbus->function == 8);
-    FAIL_IF_NOT(*modbus->subfunction == 4);
+    FAIL_IF_NOT(modbus->subfunction == 4);
 
     SigGroupCleanup(de_ctx);
     SigCleanSignatures(de_ctx);
@@ -908,7 +901,7 @@ static int DetectModbusTest11(void)
     FAIL_IF_NOT((*modbus->unit_id).min == 10);
     FAIL_IF_NOT((*modbus->unit_id).mode == mode);
     FAIL_IF_NOT(modbus->function == 8);
-    FAIL_IF_NOT((*modbus->subfunction) == 4);
+    FAIL_IF_NOT(modbus->subfunction == 4);
 
     SigGroupCleanup(de_ctx);
     SigCleanSignatures(de_ctx);
index 8ec02c99cf79e0455c55280b60f3d71732ab7c5d..2a86052ac5410674e24269b7bce7abb2ec4f81e4 100644 (file)
@@ -52,7 +52,8 @@ typedef struct DetectModbusValue_ {
 typedef struct DetectModbus_ {
     uint8_t             category;       /** < Modbus function code category to match */
     uint8_t             function;       /** < Modbus function code to match */
-    uint16_t            *subfunction;   /** < Modbus subfunction to match */
+    uint16_t            subfunction;    /** < Modbus subfunction to match */
+    bool                has_subfunction; /** < Modbus subfunction indicator */
     uint8_t             type;           /** < Modbus access type to match */
     DetectModbusValue   *unit_id;       /** < Modbus unit id to match */
     DetectModbusValue   *address;       /** < Modbus address to match */