Check library initialization before handling decoders
authorBert Vermeulen <bert@biot.com>
Mon, 18 Nov 2013 00:21:16 +0000 (01:21 +0100)
committerBert Vermeulen <bert@biot.com>
Mon, 18 Nov 2013 00:21:16 +0000 (01:21 +0100)
decoder.c
libsigrokdecode-internal.h
srd.c

index ef7e15d1bf40c8db63848a2a562bdb9972e5b354..c379d4bdc5150dab53eef390f4d8e0030a680eb5 100644 (file)
--- a/decoder.c
+++ b/decoder.c
@@ -253,6 +253,9 @@ SRD_API int srd_decoder_load(const char *module_name)
        struct srd_probe *p;
        GSList *l;
 
+       if (!srd_check_init())
+               return SRD_ERR;
+
        if (!module_name)
                return SRD_ERR_ARG;
 
@@ -446,6 +449,12 @@ SRD_API char *srd_decoder_doc_get(const struct srd_decoder *dec)
        PyObject *py_str;
        char *doc;
 
+       if (!srd_check_init())
+               return NULL;
+
+       if (!dec)
+               return NULL;
+
        if (!PyObject_HasAttrString(dec->py_mod, "__doc__"))
                return NULL;
 
@@ -495,6 +504,12 @@ SRD_API int srd_decoder_unload(struct srd_decoder *dec)
        struct srd_session *sess;
        GSList *l;
 
+       if (!srd_check_init())
+               return SRD_ERR;
+
+       if (!dec)
+               return SRD_ERR_ARG;
+
        srd_dbg("Unloading protocol decoder '%s'.", dec->name);
 
        /*
@@ -548,6 +563,9 @@ SRD_API int srd_decoder_load_all(void)
        GError *error;
        const gchar *direntry;
 
+       if (!srd_check_init())
+               return SRD_ERR;
+
        if (!(dir = g_dir_open(DECODERS_DIR, 0, &error))) {
                srd_err("Unable to open %s for reading.", DECODERS_DIR);
                return SRD_ERR_DECODERS_DIR;
index 8fd9e529dafe374ed4cc73ed51e417253a5d8a66..aa626cae12744dde2e2987c86d9ac8945086c2dc 100644 (file)
@@ -37,6 +37,7 @@ struct srd_session {
 
 /* srd.c */
 SRD_PRIV int srd_decoder_searchpath_add(const char *path);
+SRD_PRIV gboolean srd_check_init(void);
 
 /* session.c */
 SRD_PRIV int session_is_valid(struct srd_session *sess);
diff --git a/srd.c b/srd.c
index 07d79523ae859dd3d8de5d5e7d71ce0455dcf220..1c7f6f92e59fcb895940a8225477bd50047a9ac9 100644 (file)
--- a/srd.c
+++ b/srd.c
@@ -263,4 +263,14 @@ SRD_PRIV int srd_decoder_searchpath_add(const char *path)
        return SRD_OK;
 }
 
+/* @private */
+SRD_PRIV gboolean srd_check_init(void)
+{
+       if (max_session_id < 0) {
+               srd_err("Library is not initialized.");
+               return FALSE;
+       } else
+               return TRUE;
+}
+
 /** @} */