Review at  https://gerrit.osmocom.org/5016

Improve get_rate_ctr() error handling

* use explicit return instead of goto for better readability
* report back expected interval values

Change-Id: I05ca7f716342af4f7424b28216ed6c1bf2bd589f
Related: OS#2550
---
M src/ctrl/control_if.c
1 file changed, 15 insertions(+), 15 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/16/5016/1

diff --git a/src/ctrl/control_if.c b/src/ctrl/control_if.c
index 07b17c9..0c71532 100644
--- a/src/ctrl/control_if.c
+++ b/src/ctrl/control_if.c
@@ -545,15 +545,17 @@
        const struct rate_ctr *ctr;
 
        dup = talloc_strdup(cmd, cmd->variable);
-       if (!dup)
-               goto oom;
+       if (!dup) {
+               cmd->reply = "OOM";
+               return CTRL_CMD_ERROR;
+       }
 
        /* Skip over possible prefixes (net.) */
        tmp = strstr(dup, "rate_ctr");
        if (!tmp) {
                talloc_free(dup);
                cmd->reply = "rate_ctr not a token in rate_ctr command!";
-               goto err;
+               return CTRL_CMD_ERROR;
        }
 
        strtok_r(tmp, ".", &saveptr);
@@ -561,7 +563,7 @@
        if (!interval) {
                talloc_free(dup);
                cmd->reply = "Missing interval.";
-               goto err;
+               return CTRL_CMD_ERROR;
        }
 
        if (!strcmp(interval, "abs")) {
@@ -576,8 +578,8 @@
                intv = RATE_CTR_INTV_DAY;
        } else {
                talloc_free(dup);
-               cmd->reply = "Wrong interval.";
-               goto err;
+               cmd->reply = "Wrong interval. Expecting 'per_sec', 'per_min', 
'per_hour', 'per_day' or 'abs' value.";
+               return CTRL_CMD_ERROR;
        }
 
        ctr_group = strtok_r(NULL, ".", &saveptr);
@@ -586,7 +588,7 @@
                talloc_free(dup);
                cmd->reply = "Counter group must be of name.index form e. g. "
                        "e1inp.0";
-               goto err;
+               return CTRL_CMD_ERROR;
        }
 
        idx = atoi(ctr_idx);
@@ -595,7 +597,7 @@
        if (!ctrg) {
                talloc_free(dup);
                cmd->reply = "Counter group with given name and index not 
found";
-               goto err;
+               return CTRL_CMD_ERROR;
        }
 
        if (!strlen(saveptr)) {
@@ -607,20 +609,18 @@
        if (!ctr) {
                cmd->reply = "Counter name not found.";
                talloc_free(dup);
-               goto err;
+               return CTRL_CMD_ERROR;
        }
 
        talloc_free(dup);
 
        cmd->reply = talloc_asprintf(cmd, "%"PRIu64, get_rate_ctr_value(ctr, 
intv));
-       if (!cmd->reply)
-               goto oom;
+       if (!cmd->reply) {
+               cmd->reply = "OOM";
+               return CTRL_CMD_ERROR;
+       }
 
        return CTRL_CMD_REPLY;
-oom:
-       cmd->reply = "OOM";
-err:
-       return CTRL_CMD_ERROR;
 }
 
 static int set_rate_ctr(struct ctrl_cmd *cmd, void *data)

-- 
To view, visit https://gerrit.osmocom.org/5016
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I05ca7f716342af4f7424b28216ed6c1bf2bd589f
Gerrit-PatchSet: 1
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Owner: Max <[email protected]>

Reply via email to