]> sigrok.org Git - libsigrok.git/blobdiff - session_file.c
Use g_try_malloc/g_free/g_strdup consistently.
[libsigrok.git] / session_file.c
index 184dd37597c58af524bd4113b30909398947c1e5..cb99bf27ad3f4367fb97529225db63a787fca405 100644 (file)
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include "config.h"
 #include <string.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <zip.h>
 #include <glib.h>
 #include <glib/gstdio.h>
+#include "config.h"
 #include "sigrok.h"
 #include "sigrok-internal.h"
 
@@ -39,7 +39,7 @@ extern struct sr_device_plugin session_driver;
  *         SR_ERR_MALLOC upon memory allocation errors, or SR_ERR upon
  *         other errors.
  */
-int sr_session_load(const char *filename)
+SR_API int sr_session_load(const char *filename)
 {
        GKeyFile *kf;
        GPtrArray *capturefiles;
@@ -230,7 +230,7 @@ int sr_session_save(const char *filename)
                                                device->plugin_index, SR_DI_CUR_SAMPLERATE));
                                s = sr_samplerate_string(samplerate);
                                fprintf(meta, "samplerate = %s\n", s);
-                               free(s);
+                               g_free(s);
                        }
                        probecnt = 1;
                        for (p = device->probes; p; p = p->next) {
@@ -245,8 +245,14 @@ int sr_session_save(const char *filename)
                        }
 
                        /* dump datastore into logic-n */
-                       buf = malloc(ds->num_units * ds->ds_unitsize +
+                       buf = g_try_malloc(ds->num_units * ds->ds_unitsize +
                                   DATASTORE_CHUNKSIZE);
+                       if (!buf) {
+                               sr_err("session file: %s: buf malloc failed",
+                                      __func__);
+                               return SR_ERR_MALLOC;
+                       }
+
                        bufcnt = 0;
                        for (d = ds->chunklist; d; d = d->next) {
                                memcpy(buf + bufcnt, d->data,