bug-indent
[Top][All Lists]
Advanced

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

[PATCH 3/7] Don't cast the result of malloc() or realloc()


From: Tim Hentenaar
Subject: [PATCH 3/7] Don't cast the result of malloc() or realloc()
Date: Sun, 5 Jul 2015 15:10:50 +0200

---
 src/args.c        |  4 ++--
 src/backup.c      |  6 ++---
 src/code_io.c     | 12 +++++-----
 src/handletoken.c | 70 +++++++++++++++++++++----------------------------------
 src/indent.c      |  9 ++++---
 src/lexi.c        |  5 ++--
 src/output.c      |  2 +-
 src/parse.c       | 33 +++++++++++++-------------
 8 files changed, 62 insertions(+), 79 deletions(-)

diff --git a/src/args.c b/src/args.c
index 77e14dd..a832133 100644
--- a/src/args.c
+++ b/src/args.c
@@ -1248,7 +1248,7 @@ char * set_profile(void)
          scan_profile (f, INDENT_PROFILE);
          (void) fclose (f);
 
-         fname = xmalloc (len);
+         fname = xmalloc(len);
          strcpy(fname, "./");
          (void) strcat (fname, INDENT_PROFILE);
       }
@@ -1258,7 +1258,7 @@ char * set_profile(void)
     
          if (homedir)
          {
-            fname = xmalloc (strlen (homedir) + strlen(PROFILE_FORMAT) + 
sizeof (prof));
+            fname = xmalloc(strlen (homedir) + strlen(PROFILE_FORMAT) + sizeof 
(prof));
             sprintf (fname, PROFILE_FORMAT, homedir, prof);
                 
             if ((f = fopen (fname, "r")) != NULL)
diff --git a/src/backup.c b/src/backup.c
index ba05138..1a76c0b 100644
--- a/src/backup.c
+++ b/src/backup.c
@@ -194,7 +194,7 @@ static char * simple_backup_name (
 {
     char *backup_name;
 
-    backup_name = xmalloc (strlen (pathname) + strlen (simple_backup_suffix) + 
2);
+    backup_name = xmalloc(strlen (pathname) + strlen (simple_backup_suffix) + 
2);
     sprintf (backup_name, "%s%s", pathname, simple_backup_suffix);
     return backup_name;
 }
@@ -305,7 +305,7 @@ static int max_version (
         char *dirname;
 
         filename = p + 1;
-        dirname = xmalloc (dirlen + 1);
+        dirname = xmalloc(dirlen + 1);
         strncpy (dirname, pathname, (dirlen));
         dirname[dirlen] = '\0';
         version = highest_version (filename, dirname);
@@ -354,7 +354,7 @@ static char * generate_backup_filename (
           else
           {
              last_numbered_version++;
-             backup_name = xmalloc (strlen (pathname) + 16);
+             backup_name = xmalloc(strlen (pathname) + 16);
 
              if (backup_name)
              {
diff --git a/src/code_io.c b/src/code_io.c
index 016eb86..d48756b 100644
--- a/src/code_io.c
+++ b/src/code_io.c
@@ -306,14 +306,14 @@ extern file_buffer_ty * read_file(
     
     if (fileptr.data != 0)
     {
-        fileptr.data = (char *) xrealloc (fileptr.data,
-                                          (unsigned) file_stats->st_size + 2); 
/* add 1 for '\0' and 1 for
+        fileptr.data = xrealloc(fileptr.data,
+                                 (unsigned) file_stats->st_size + 2); /* add 1 
for '\0' and 1 for
                                                                                
 * potential final added
                                                                                
 * newline. */
     }
     else
     {
-        fileptr.data = (char *) xmalloc ((unsigned) file_stats->st_size + 2); 
/* add 1 for '\0' and 1 for
+        fileptr.data = xmalloc((unsigned) file_stats->st_size + 2); /* add 1 
for '\0' and 1 for
                                                                                
* potential final added
                                                                                
* newline. */
     }
@@ -359,11 +359,11 @@ extern file_buffer_ty * read_file(
 
     if (fileptr.name != NULL)
     {
-        fileptr.name = (char *) xrealloc (fileptr.name, (unsigned) namelen + 
1);
+        fileptr.name = xrealloc(fileptr.name, (unsigned) namelen + 1);
     }
     else
     {
-        fileptr.name = (char *) xmalloc (namelen + 1);
+        fileptr.name = xmalloc(namelen + 1);
     }
     
     memcpy(fileptr.name, filename, namelen);
@@ -405,7 +405,7 @@ file_buffer_ty * read_stdin(void)
         free (stdinptr.data);
     }
 
-    stdinptr.data = (char *) xmalloc (size + 1);
+    stdinptr.data = xmalloc(size + 1);
     stdinptr.size = 0;
     
     p = stdinptr.data;
diff --git a/src/handletoken.c b/src/handletoken.c
index 7bdb7f8..ed3aef4 100644
--- a/src/handletoken.c
+++ b/src/handletoken.c
@@ -104,7 +104,7 @@ extern void check_code_size(void)
     if (e_code >= l_code)                               
     {                                                   
         int nsize = l_code - s_code + 400;               
-        codebuf   = (char *) xrealloc (codebuf, nsize);  
+        codebuf   = xrealloc(codebuf, nsize);  
         e_code    = codebuf + (e_code - s_code) + 1; 
         l_code    = codebuf + nsize - 5; 
         s_code    = codebuf + 1; 
@@ -120,7 +120,7 @@ static void check_lab_size(void)
     if (e_lab >= l_lab)
     {
         int nsize  = l_lab - s_lab + 400;               
-        labbuf = (char *) xrealloc (labbuf, nsize); 
+        labbuf = xrealloc(labbuf, nsize); 
         e_lab  = labbuf + (e_lab - s_lab) + 1;        
         l_lab  = labbuf + nsize - 5;                
         s_lab  = labbuf + 1;                        
@@ -342,9 +342,8 @@ static void handle_token_lparen(
     {
         parser_state_tos->paren_indents_size *= 2;
         parser_state_tos->paren_indents =
-                (short *) xrealloc ((char *) parser_state_tos->paren_indents,
-                                    parser_state_tos->paren_indents_size *
-                                    sizeof (short));
+                xrealloc(parser_state_tos->paren_indents,
+                         parser_state_tos->paren_indents_size * sizeof(short));
     }
 
     parser_state_tos->paren_depth++;
@@ -1168,9 +1167,8 @@ static void handle_token_lbrace(
         if (parser_state_tos->dec_nest >= di_stack_alloc)
         {
             di_stack_alloc *= 2;
-            di_stack =
-                    (int *) xrealloc ((char *) di_stack,
-                                      di_stack_alloc * sizeof (*di_stack));
+            di_stack = xrealloc(di_stack,
+                                di_stack_alloc * sizeof(*di_stack));
         }
 
         di_stack[parser_state_tos->dec_nest++] = *dec_ind;
@@ -1230,10 +1228,9 @@ static void handle_token_lbrace(
         {
             parser_state_tos->paren_indents_size *= 2;
             parser_state_tos->paren_indents =
-                    (short *) xrealloc ((char *) parser_state_tos->
-                                        paren_indents,
-                                        parser_state_tos->paren_indents_size *
-                                        sizeof (short));
+                    xrealloc(parser_state_tos->paren_indents,
+                             parser_state_tos->paren_indents_size *
+                             sizeof(short));
         }
 
         ++parser_state_tos->paren_depth;
@@ -2006,45 +2003,30 @@ static void handle_token_preesc(
 
             parser_state_ty *new;
 
-            new = (parser_state_ty *)
-                    xmalloc (sizeof (parser_state_ty));
-            (void) memcpy (new, parser_state_tos,
-                           sizeof (parser_state_ty));
+            new = xmalloc(sizeof(parser_state_ty));
+            memcpy(new, parser_state_tos, sizeof(parser_state_ty));
 
             /* We need to copy the dynamically allocated arrays in the
              * struct parser_state too.  */
 
-            new->p_stack =
-                    (codes_ty *) xmalloc (parser_state_tos->p_stack_size *
-                                          sizeof (codes_ty));
-
-            (void) memcpy (new->p_stack, parser_state_tos->p_stack,
-                           (parser_state_tos->p_stack_size *
-                            sizeof (codes_ty)));
+            new->p_stack = xmalloc(parser_state_tos->p_stack_size *
+                                   sizeof(codes_ty));
+            memcpy(new->p_stack, parser_state_tos->p_stack,
+                   (parser_state_tos->p_stack_size * sizeof(codes_ty)));
 
-            new->il =
-                    (int *) xmalloc (parser_state_tos->p_stack_size *
-                                     sizeof (int));
-
-            (void) memcpy (new->il, parser_state_tos->il,
-                           parser_state_tos->p_stack_size *
-                           sizeof (int));
+            new->il = xmalloc(parser_state_tos->p_stack_size *
+                              sizeof (int));
+            memcpy(new->il, parser_state_tos->il,
+                   parser_state_tos->p_stack_size * sizeof(int));
 
-            new->cstk =
-                    (int *) xmalloc (parser_state_tos->p_stack_size *
-                                     sizeof (int));
+            new->cstk = xmalloc(parser_state_tos->p_stack_size * sizeof(int));
+            memcpy(new->cstk, parser_state_tos->cstk,
+                   parser_state_tos->p_stack_size * sizeof(int));
 
-            (void) memcpy (new->cstk, parser_state_tos->cstk,
-                           parser_state_tos->p_stack_size *
-                           sizeof (int));
-
-            new->paren_indents =
-                    (short *) xmalloc (parser_state_tos->paren_indents_size *
-                                       sizeof (short));
-            (void) memcpy (new->paren_indents,
-                           parser_state_tos->paren_indents,
-                           (parser_state_tos->paren_indents_size *
-                            sizeof (short)));
+            new->paren_indents = xmalloc(parser_state_tos->paren_indents_size *
+                                         sizeof (short));
+            memcpy(new->paren_indents,parser_state_tos->paren_indents,
+                   (parser_state_tos->paren_indents_size * sizeof(short)));
 
             new->next = parser_state_tos;
             parser_state_tos = new;
diff --git a/src/indent.c b/src/indent.c
index 98b02a2..f38dedd 100644
--- a/src/indent.c
+++ b/src/indent.c
@@ -868,10 +868,9 @@ static exit_values_ty process_args (
                     if (input_files > max_input_files)
                     {
                         max_input_files = 2 * max_input_files;
-                        in_file_names =
-                                (char **) xrealloc ((char *) in_file_names,
-                                                    (max_input_files *
-                                                     sizeof (char *)));
+                        in_file_names = xrealloc(in_file_names,
+                                                 (max_input_files *
+                                                  sizeof(char *)));
                     }
                 }
 
@@ -1099,7 +1098,7 @@ int main(
     exit_status = total_success;
 
     input_files = 0;
-    in_file_names = (char **) xmalloc(max_input_files * sizeof (char *));
+    in_file_names = xmalloc(max_input_files * sizeof(char *));
 
     set_defaults();
 
diff --git a/src/lexi.c b/src/lexi.c
index ca527b1..61be63d 100644
--- a/src/lexi.c
+++ b/src/lexi.c
@@ -1259,14 +1259,15 @@ extern void addkey (
     {
        if (user_specials == 0)
        {
-          user_specials = (templ_ty *) xmalloc (5 * sizeof (templ_ty));
+          user_specials = xmalloc(5 * sizeof(templ_ty));
           user_specials_max = 5;
           user_specials_idx = 0;
        }
        else if (user_specials_idx == user_specials_max)
        {
           user_specials_max += 5;
-          user_specials = (templ_ty *) xrealloc ((char *) user_specials, 
user_specials_max * sizeof (templ_ty));
+          user_specials = xrealloc(user_specials,
+                                   user_specials_max * sizeof(templ_ty));
        }
        else
        {
diff --git a/src/output.c b/src/output.c
index 047f85b..71049ce 100644
--- a/src/output.c
+++ b/src/output.c
@@ -328,7 +328,7 @@ void set_buf_break (
 
     /* Store the position of `e_code' as the place to break this line. */
 
-    bb = (buf_break_st_ty *) xmalloc (sizeof (buf_break_st_ty));
+    bb = xmalloc(sizeof(buf_break_st_ty));
     bb->offset = e_code - s_code;
     bb->level = level;
     bb->target_col = target_col;
diff --git a/src/parse.c b/src/parse.c
index 6e41bb5..a547d98 100644
--- a/src/parse.c
+++ b/src/parse.c
@@ -62,29 +62,28 @@ parser_state_ty *parser_state_tos = NULL;
 
 extern void init_parser(void)
 {
-    parser_state_tos = (parser_state_ty *) xmalloc (sizeof (parser_state_ty));
+    parser_state_tos = xmalloc(sizeof (parser_state_ty));
 
     /* GDB_HOOK_parser_state_tos */
     parser_state_tos->p_stack_size  = INITIAL_STACK_SIZE;
-    parser_state_tos->p_stack       = (codes_ty *) xmalloc (INITIAL_STACK_SIZE 
* sizeof (codes_ty));
-    parser_state_tos->il            = (int *) xmalloc (INITIAL_STACK_SIZE * 
sizeof (int));
-    parser_state_tos->cstk          = (int *) xmalloc (INITIAL_STACK_SIZE * 
sizeof (int));
+    parser_state_tos->p_stack       = xmalloc(INITIAL_STACK_SIZE * sizeof 
(codes_ty));
+    parser_state_tos->il            = xmalloc(INITIAL_STACK_SIZE * sizeof 
(int));
+    parser_state_tos->cstk          = xmalloc(INITIAL_STACK_SIZE * sizeof 
(int));
     parser_state_tos->paren_indents_size = 8;
-    parser_state_tos->paren_indents = (short *) xmalloc 
(parser_state_tos->paren_indents_size * sizeof (short));
+    parser_state_tos->paren_indents = 
xmalloc(parser_state_tos->paren_indents_size * sizeof (short));
 
     /* Although these are supposed to grow if we reach the end,
      * I can find no place in the code which does this. */
-
-    combuf = (char *) xmalloc (INITIAL_BUFFER_SIZE);
-    labbuf = (char *) xmalloc (INITIAL_BUFFER_SIZE);
-    codebuf = (char *) xmalloc (INITIAL_BUFFER_SIZE);
+    combuf = xmalloc(INITIAL_BUFFER_SIZE);
+    labbuf = xmalloc(INITIAL_BUFFER_SIZE);
+    codebuf = xmalloc(INITIAL_BUFFER_SIZE);
 
     save_com.size = INITIAL_BUFFER_SIZE;
-    save_com.end = save_com.ptr = xmalloc (save_com.size);
+    save_com.end = save_com.ptr = xmalloc(save_com.size);
     save_com.len = save_com.column = 0;
 
     di_stack_alloc = 2;
-    di_stack = (int *) xmalloc (di_stack_alloc * sizeof (*di_stack));
+    di_stack = xmalloc(di_stack_alloc * sizeof (*di_stack));
 }
 
 /**
@@ -204,12 +203,14 @@ extern int inc_pstack(void)
     {
         parser_state_tos->p_stack_size *= 2;
         parser_state_tos->p_stack =
-                (codes_ty *) xrealloc ((char *) parser_state_tos->p_stack,
-                                       parser_state_tos->p_stack_size * sizeof 
(codes_ty));
+                xrealloc(parser_state_tos->p_stack,
+                         parser_state_tos->p_stack_size * sizeof(codes_ty));
         parser_state_tos->il =
-                (int *) xrealloc ((char *) parser_state_tos->il, 
parser_state_tos->p_stack_size * sizeof (int));
+                xrealloc(parser_state_tos->il,
+                          parser_state_tos->p_stack_size * sizeof(int));
         parser_state_tos->cstk =
-                (int *) xrealloc ((char *) parser_state_tos->cstk, 
parser_state_tos->p_stack_size * sizeof (int));
+                xrealloc(parser_state_tos->cstk,
+                         parser_state_tos->p_stack_size * sizeof(int));
     }
 
     parser_state_tos->cstk[parser_state_tos->tos] = 
parser_state_tos->cstk[parser_state_tos->tos - 1];
@@ -231,7 +232,7 @@ extern void debug_init(void)
 {
     int size = ((int) number_of_codes) * sizeof (char *);
 
-    debug_symbol_strings = (char **) xmalloc (size);
+    debug_symbol_strings = xmalloc (size);
 
     debug_symbol_strings[code_eof]      = "code_eof";
     debug_symbol_strings[newline]       = "newline";
-- 
2.3.6




reply via email to

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