libcdio-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Libcdio-devel] [PATCH 1/2] add asserts to test memory allocations


From: Pete Batard
Subject: [Libcdio-devel] [PATCH 1/2] add asserts to test memory allocations
Date: Mon, 13 Mar 2017 12:09:58 +0100
User-agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0

Also use the common breakdown for calloc() parameters, add
a missing closing parenthesis in a log statement and NUL
terminate a string after the srtncpy() call.

From 5bcdab785eb41fa2379774a7434ac7774d9527a0 Mon Sep 17 00:00:00 2001
From: Pete Batard <address@hidden>
Date: Mon, 13 Mar 2017 11:42:30 +0100
Subject: [PATCH 1/2] add asserts to test memory allocations

Also use the common breakdown for calloc() parameters, add
a missing closing parenthesis in a log statement and NUL
terminate a string after the srtncpy() call.
---
 lib/driver/_cdio_stdio.c  |  2 ++
 lib/driver/_cdio_stream.c |  1 +
 lib/driver/ds.c           |  2 ++
 lib/driver/utf8.c         |  3 +++
 lib/driver/util.c         | 18 ++++++++++--------
 lib/iso9660/iso9660_fs.c  | 11 +++++------
 lib/udf/udf_fs.c          |  2 +-
 7 files changed, 24 insertions(+), 15 deletions(-)

diff --git a/lib/driver/_cdio_stdio.c b/lib/driver/_cdio_stdio.c
index 805cc69..1d46b15 100644
--- a/lib/driver/_cdio_stdio.c
+++ b/lib/driver/_cdio_stdio.c
@@ -45,6 +45,7 @@
 #include <cdio/util.h>
 #include "_cdio_stream.h"
 #include "_cdio_stdio.h"
+#include "cdio_assert.h"
 
 /* On 32 bit platforms, fseek can only access streams of 2 GB or less.
    Prefer fseeko/fseeko64, that take a 64 bit offset when LFS is enabled */
@@ -265,6 +266,7 @@ cdio_stdio_new(const char pathname[])
     }
 
   ud = calloc (1, sizeof (_UserData));
+  cdio_assert (ud != NULL);
 
   ud->pathname = pathdup;
   ud->st_size  = statbuf.st_size; /* let's hope it doesn't change... */
diff --git a/lib/driver/_cdio_stream.c b/lib/driver/_cdio_stream.c
index 79dab94..f8da21e 100644
--- a/lib/driver/_cdio_stream.c
+++ b/lib/driver/_cdio_stream.c
@@ -99,6 +99,7 @@ cdio_stream_new(void *user_data, const 
cdio_stream_io_functions *funcs)
   CdioDataSource_t *new_obj;
 
   new_obj = calloc (1, sizeof (CdioDataSource_t));
+  cdio_assert (new_obj != NULL);
 
   new_obj->user_data = user_data;
   memcpy(&(new_obj->op), funcs, sizeof(cdio_stream_io_functions));
diff --git a/lib/driver/ds.c b/lib/driver/ds.c
index cf53e54..c9abec2 100644
--- a/lib/driver/ds.c
+++ b/lib/driver/ds.c
@@ -85,6 +85,7 @@ _cdio_list_prepend (CdioList_t *p_list, void *p_data)
   cdio_assert (p_list != NULL);
 
   p_new_node = calloc (1, sizeof (CdioListNode_t));
+  cdio_assert (p_new_node != NULL);
 
   p_new_node->list = p_list;
   p_new_node->next = p_list->begin;
@@ -109,6 +110,7 @@ _cdio_list_append (CdioList_t *p_list, void *p_data)
   else
     {
       CdioListNode_t *p_new_node = calloc (1, sizeof (CdioListNode_t));
+      cdio_assert (p_new_node != NULL);
 
       p_new_node->list = p_list;
       p_new_node->next = NULL;
diff --git a/lib/driver/utf8.c b/lib/driver/utf8.c
index b87cb02..28353d0 100644
--- a/lib/driver/utf8.c
+++ b/lib/driver/utf8.c
@@ -42,6 +42,7 @@
 #include <cdio/utf8.h>
 #include <cdio/logging.h>
 #include <cdio/memory.h>
+#include "cdio_assert.h"
 
 /* Windows requires some basic UTF-8 support outside of Joliet */
 #if defined(_WIN32)
@@ -300,6 +301,7 @@ bool cdio_charset_from_utf8(cdio_utf8_t * src, char ** dst,
   /* Perform byte reversal */
   len = wcslen(le_dst);
   *dst = (char*)calloc(len+1, sizeof(wchar_t));
+  cdio_assert(*dst != NULL);
   for (i=0; i<2*len; i++) {
     (*dst)[i] = ((char*)le_dst)[i+1];
     (*dst)[i+1] = ((char*)le_dst)[i];
@@ -333,6 +335,7 @@ bool cdio_charset_to_utf8(const char *src, size_t src_len, 
cdio_utf8_t **dst,
 
   /* Perform byte reversal */
   le_src = (wchar_t*)malloc(2*src_len+2);
+  cdio_assert(le_src != NULL);
   for (i=0; i<src_len; i++) {
     ((char*)le_src)[2*i] = src[2*i+1];
     ((char*)le_src)[2*i+1] = src[2*i];
diff --git a/lib/driver/util.c b/lib/driver/util.c
index bc2e61a..5108457 100644
--- a/lib/driver/util.c
+++ b/lib/driver/util.c
@@ -62,7 +62,7 @@ void
 _cdio_strfreev(char **strv)
 {
   int n;
-  
+
   cdio_assert (strv != NULL);
 
   for(n = 0; strv[n]; n++)
@@ -88,14 +88,15 @@ _cdio_strsplit(const char str[], char delim) /* fixme -- 
non-reentrant */
 
   n = 1;
   p = _str;
-  while(*p) 
+  while(*p)
     if (*(p++) == delim)
       n++;
 
-  strv = calloc (1, sizeof (char *) * (n+1));
-  
+  strv = calloc (n+1, sizeof (char *));
+  cdio_assert (strv != NULL);
+
   n = 0;
-  while((p = strtok(n ? NULL : _str, _delim)) != NULL) 
+  while((p = strtok(n ? NULL : _str, _delim)) != NULL)
     strv[n++] = strdup(p);
 
   free(_str);
@@ -111,9 +112,10 @@ _cdio_memdup (const void *mem, size_t count)
   if (mem)
     {
       new_mem = calloc (1, count);
+      cdio_assert (new_mem != NULL);
       memcpy (new_mem, mem, count);
     }
-  
+
   return new_mem;
 }
 
@@ -141,7 +143,7 @@ _cdio_strdup_upper (const char str[])
 /* Convert MinGW/MSYS paths that start in "/c/..." to "c:/..."
    so that they can be used with fopen(), stat(), etc.
    Returned string must be freed by the caller using cdio_free().*/
-char * 
+char *
 _cdio_strdup_fixpath (const char path[])
 {
   char *new_path = NULL;
@@ -180,7 +182,7 @@ const char *cdio_version_string = CDIO_VERSION;
 const unsigned int libcdio_version_num = LIBCDIO_VERSION_NUM;
 
 
-/* 
+/*
  * Local variables:
  *  c-file-style: "gnu"
  *  tab-width: 8
diff --git a/lib/iso9660/iso9660_fs.c b/lib/iso9660/iso9660_fs.c
index 06628ae..8758a23 100644
--- a/lib/iso9660/iso9660_fs.c
+++ b/lib/iso9660/iso9660_fs.c
@@ -270,7 +270,7 @@ check_pvd (const iso9660_pvd_t *p_pvd, cdio_log_level_t 
log_level)
   if (strncmp (p_pvd->id, ISO_STANDARD_ID, strlen (ISO_STANDARD_ID)))
     {
       cdio_log (log_level, "unexpected ID encountered (expected `"
-               ISO_STANDARD_ID "', got `%.5s'", p_pvd->id);
+               ISO_STANDARD_ID "', got `%.5s')", p_pvd->id);
       return false;
     }
   return true;
@@ -958,8 +958,10 @@ _fs_stat_traverse (const CdIo_t *p_cdio, const 
iso9660_stat_t *_root,
     {
       unsigned int len=sizeof(iso9660_stat_t) + strlen(_root->filename)+1;
       p_stat = calloc(1, len);
+      cdio_assert (p_stat != NULL);
       memcpy(p_stat, _root, len);
       p_stat->rr.psz_symlink = calloc(1, p_stat->rr.i_symlink_max);
+      cdio_assert (p_stat->rr.psz_symlink != NULL);
       memcpy(p_stat->rr.psz_symlink, _root->rr.psz_symlink,
             p_stat->rr.i_symlink_max);
       return p_stat;
@@ -1053,13 +1055,10 @@ _fs_iso_stat_traverse (iso9660_t *p_iso, const 
iso9660_stat_t *_root,
       iso9660_stat_t *p_stat;
       unsigned int len=sizeof(iso9660_stat_t) + strlen(_root->filename)+1;
       p_stat = calloc(1, len);
-      if (!p_stat)
-        {
-        cdio_warn("Couldn't calloc(1, %d)", len);
-        return NULL;
-        }
+      cdio_assert (p_stat != NULL);
       memcpy(p_stat, _root, len);
       p_stat->rr.psz_symlink = calloc(1, p_stat->rr.i_symlink_max);
+      cdio_assert (p_stat->rr.psz_symlink != NULL);
       memcpy(p_stat->rr.psz_symlink, _root->rr.psz_symlink,
             p_stat->rr.i_symlink_max);
       return p_stat;
diff --git a/lib/udf/udf_fs.c b/lib/udf/udf_fs.c
index a0a3903..702268b 100644
--- a/lib/udf/udf_fs.c
+++ b/lib/udf/udf_fs.c
@@ -254,8 +254,8 @@ udf_fopen(udf_dirent_t *p_udf_root, const char *psz_name)
     /* file position must be reset when accessing a new file */
     p_udf_root->p_udf->i_position = 0;
 
-    tokenline[udf_MAX_PATHLEN-1] = '\0';
     strncpy(tokenline, psz_name, udf_MAX_PATHLEN-1);
+    tokenline[udf_MAX_PATHLEN-1] = '\0';
     psz_token = strtok(tokenline, udf_PATH_DELIMITERS);
     if (psz_token) {
       udf_dirent_t *p_udf_dirent =
-- 
2.8.1.windows.1


reply via email to

[Prev in Thread] Current Thread [Next in Thread]