]> sigrok.org Git - libsigrokdecode.git/blobdiff - controller.c
srd: deal with invalid probe specifications better
[libsigrokdecode.git] / controller.c
index c696dd370850894d36b9cc0ae30b795b3f3ceaad..845208a7c2f5041a9e471ba101f58b932ecb56c0 100644 (file)
@@ -266,15 +266,13 @@ err_out:
        Py_XDECREF(py_dec_options);
        if (key)
                g_free(key);
-       if (PyErr_Occurred()) {
-               srd_dbg("srd: stray exception!");
-               PyErr_Print();
-               PyErr_Clear();
-       }
+       if (PyErr_Occurred())
+               catch_exception("srd: stray exception in srd_instance_set_options()");
 
        return ret;
 }
 
+/* Helper GComparefunc for g_slist_find_custom() in srd_instance_set_probes() */
 static gint compare_probe_id(struct srd_probe *a, char *probe_id)
 {
 
@@ -298,7 +296,7 @@ int srd_instance_set_probes(struct srd_decoder_instance *di,
        GSList *sl;
        struct srd_probe *p;
        int *new_probemap, new_probenum;
-       char *probe_id;
+       char *probe_id, *probenum_str;
 
        if (g_hash_table_size(new_probes) == 0)
                /* No probes provided. */
@@ -320,7 +318,14 @@ int srd_instance_set_probes(struct srd_decoder_instance *di,
 
        for (l = g_hash_table_get_keys(new_probes); l; l = l->next) {
                probe_id = l->data;
-               new_probenum = strtol(g_hash_table_lookup(new_probes, probe_id), NULL, 10);
+               probenum_str = g_hash_table_lookup(new_probes, probe_id);
+               if (!probenum_str) {
+                       /* Probe name was specified without a value. */
+                       srd_err("No probe number was specified for %s.", probe_id);
+                       g_free(new_probemap);
+                       return SRD_ERR_ARG;
+               }
+               new_probenum = strtol(probenum_str, NULL, 10);
                if (!(sl = g_slist_find_custom(di->decoder->probes, probe_id,
                                (GCompareFunc)compare_probe_id))) {
                        /* Fall back on optional probes. */
@@ -393,7 +398,7 @@ struct srd_decoder_instance *srd_instance_new(const char *decoder_id,
        /* Create a new instance of this decoder class. */
        if (!(di->py_instance = PyObject_CallObject(dec->py_dec, NULL))) {
                if (PyErr_Occurred())
-                       PyErr_Print();
+                       catch_exception("failed to create %s instance: ", decoder_id);
                g_free(di->dec_probemap);
                g_free(di);
                return NULL;
@@ -462,15 +467,13 @@ int srd_instance_start(struct srd_decoder_instance *di, PyObject *args)
 
        if (!(py_name = PyUnicode_FromString("start"))) {
                srd_err("Unable to build python object for 'start'.");
-               if (PyErr_Occurred())
-                       PyErr_Print();
+               catch_exception("Protocol decoder instance %s: ", di->instance_id);
                return SRD_ERR_PYTHON;
        }
 
        if (!(py_res = PyObject_CallMethodObjArgs(di->py_instance,
                        py_name, args, NULL))) {
-               if (PyErr_Occurred())
-                       PyErr_Print();
+               catch_exception("Protocol decoder instance %s: ", di->instance_id);
                return SRD_ERR_PYTHON;
        }
 
@@ -532,9 +535,7 @@ int srd_instance_decode(uint64_t start_samplenum,
        end_samplenum = start_samplenum + inbuflen / di->data_unitsize;
        if (!(py_res = PyObject_CallMethod(di->py_instance, "decode",
                        "KKO", logic->start_samplenum, end_samplenum, logic))) {
-               if (PyErr_Occurred())
-                       PyErr_Print(); /* Returns void. */
-
+               catch_exception("Protocol decoder instance %s: ", di->instance_id);
                return SRD_ERR_PYTHON; /* TODO: More specific error? */
        }
        Py_DecRef(py_res);
@@ -604,6 +605,41 @@ int srd_session_feed(uint64_t start_samplenum, uint8_t *inbuf, uint64_t inbuflen
 }
 
 
+int srd_register_callback(int output_type, void *cb)
+{
+       struct srd_pd_callback *pd_cb;
+
+       srd_dbg("srd: registering new callback for output type %d", output_type);
+
+       if (!(pd_cb = g_try_malloc(sizeof(struct srd_pd_callback))))
+               return SRD_ERR_MALLOC;
+
+       pd_cb->output_type = output_type;
+       pd_cb->callback = cb;
+       callbacks = g_slist_append(callbacks, pd_cb);
+
+       return SRD_OK;
+}
+
+void *srd_find_callback(int output_type)
+{
+       GSList *l;
+       struct srd_pd_callback *pd_cb;
+       void *(cb);
+
+       cb = NULL;
+       for (l = callbacks; l; l = l->next) {
+               pd_cb = l->data;
+               if (pd_cb->output_type == output_type) {
+                       cb = pd_cb->callback;
+                       break;
+               }
+       }
+
+       return cb;
+}
+
+
 /* This is the backend function to python sigrokdecode.add() call. */
 int pd_add(struct srd_decoder_instance *di, int output_type,
                char *proto_id)
@@ -646,37 +682,3 @@ struct srd_decoder_instance *get_di_by_decobject(void *decobject)
        return NULL;
 }
 
-int srd_register_callback(int output_type, void *cb)
-{
-       struct srd_pd_callback *pd_cb;
-
-       srd_dbg("srd: registering new callback for output type %d", output_type);
-
-       if (!(pd_cb = g_try_malloc(sizeof(struct srd_pd_callback))))
-               return SRD_ERR_MALLOC;
-
-       pd_cb->output_type = output_type;
-       pd_cb->callback = cb;
-       callbacks = g_slist_append(callbacks, pd_cb);
-
-       return SRD_OK;
-}
-
-void *srd_find_callback(int output_type)
-{
-       GSList *l;
-       struct srd_pd_callback *pd_cb;
-       void *(cb);
-
-       cb = NULL;
-       for (l = callbacks; l; l = l->next) {
-               pd_cb = l->data;
-               if (pd_cb->output_type == output_type) {
-                       cb = pd_cb->callback;
-                       break;
-               }
-       }
-
-       return cb;
-}
-