[MDEV-8412] [PATCH] Replace calls to bzero() with memset() Created: 2015-07-02  Updated: 2018-02-11  Resolved: 2018-02-11

Status: Closed
Project: MariaDB Server
Component/s: Compiling
Affects Version/s: 10.0.20
Fix Version/s: N/A

Type: Bug Priority: Minor
Reporter: Bill Parker Assignee: Sergei Golubchik
Resolution: Won't Do Votes: 0
Labels: Patch
Environment:

FreeBSD/Linux (32-64 bit platforms), all


Attachments: File completion_hash.cc.patch     File dbug.c.patch     File mysql.cc.patch     File mysql_plugin.c.patch     File mysql_upgrade.c.patch     File mysqlbinlog.cc.patch     File mysqldump.c.patch     File mysqlslap.c.patch     File mysqltest.cc.patch    

 Description   

Subj: Convert bzero() to memset() in directory 'mariadb-10.0.20/client'

Hello All,

In reviewing code in MariaDB 10.0.2x, I found several instances
where the deprecated library call 'bzero()' is referenced in various
files. The patch file(s) below replace calls to bzero() with memset().

The patch files are listed below and attached to this bug report:

 
--- completion_hash.cc.orig     2015-07-02 09:41:47.000000000 -0700
+++ completion_hash.cc  2015-07-02 09:43:54.000000000 -0700
@@ -205,7 +205,7 @@
 void completion_hash_clean(HashTable *ht)
 {
   free_root(&ht->mem_root,MYF(0));
-  bzero((char*) ht->arBuckets,ht->nTableSize*sizeof(Bucket *));
+  memset((char *)ht->arBuckets, 0, ht->nTableSize*sizeof(Bucket *)); 
 }
 

 
--- mysql.cc.orig       2015-07-02 09:47:14.000000000 -0700
+++ mysql.cc    2015-07-02 09:48:35.000000000 -0700
@@ -1199,7 +1199,7 @@
   glob_buffer.realloc(512);
   completion_hash_init(&ht, 128);
   init_alloc_root(&hash_mem_root, 16384, 0, MYF(0));
-  bzero((char*) &mysql, sizeof(mysql));
+  memset((char *) &mysql, 0, sizeof(mysql));
   if (sql_connect(current_host,current_db,current_user,opt_password,
                  opt_silent))
   {
@@ -4212,7 +4212,7 @@
   my_bool save_rehash= opt_rehash;
   int error;
 
-  bzero(buff, sizeof(buff));
+  memset(buff, 0, sizeof(buff));
   if (buffer)
   {
     /*
@@ -4360,7 +4360,7 @@
   char *tmp, buff[FN_REFLEN + 1];
   int select_db;
 
-  bzero(buff, sizeof(buff));
+  memset(buff, 0, sizeof(buff));
   strmake_buf(buff, line);
   tmp= get_arg(buff, 0);
   if (!tmp || !*tmp)
   

 
--- mysql_plugin.c.orig 2015-07-02 09:50:00.000000000 -0700
+++ mysql_plugin.c      2015-07-02 09:50:28.000000000 -0700
@@ -321,7 +321,7 @@
   int ret= 0;
   FILE *file= 0;
 
-  bzero(tool_path, FN_REFLEN);
+  memset(tool_path, 0, FN_REFLEN);
   if ((error= find_tool("my_print_defaults" FN_EXEEXT, tool_path)))
     goto exit;
   else
   

 
--- mysql_upgrade.c.orig        2015-07-02 09:52:08.000000000 -0700
+++ mysql_upgrade.c     2015-07-02 09:52:32.000000000 -0700
@@ -664,7 +664,7 @@
   if (!(in= my_fopen(upgrade_info_file, O_RDONLY, MYF(0))))
     return 0; /* Could not open file => not sure */
 
-  bzero(buf, sizeof(buf));
+  memset(buf, 0, sizeof(buf));
   if (!fgets(buf, sizeof(buf), in))
   {
     /* Ignore, will be detected by strncmp() below */
	 

 
--- mysqlbinlog.cc.orig 2015-07-02 09:54:02.000000000 -0700
+++ mysqlbinlog.cc      2015-07-02 09:55:27.000000000 -0700
@@ -305,7 +305,7 @@
       {
         my_free(ptr->fname);
         delete ptr->event;
-        bzero((char *)ptr, sizeof(File_name_record));
+                               memset((char *)ptr, 0, sizeof(File_name_record));
       }
     }
 
@@ -336,7 +336,7 @@
         return 0;
       ptr= dynamic_element(&file_names, file_id, File_name_record*);
       if ((res= ptr->event))
-        bzero((char *)ptr, sizeof(File_name_record));
+                               memset((char *)ptr, 0, sizeof(File_name_record));
       return res;
     }
 
@@ -366,7 +366,7 @@
       if (!ptr->event)
       {
         res= ptr->fname;
-        bzero((char *)ptr, sizeof(File_name_record));
+                               memset((char *)ptr, 0, sizeof(File_name_record));
       }
       return res;
     }
 

 
--- mysqldump.c.orig    2015-07-02 09:56:47.000000000 -0700
+++ mysqldump.c 2015-07-02 09:57:25.000000000 -0700
@@ -5809,7 +5809,7 @@
   sf_leaking_memory=1; /* don't report memory leaks on early exits */
   compatible_mode_normal_str[0]= 0;
   default_charset= (char *)mysql_universal_client_charset;
-  bzero((char*) &ignore_table, sizeof(ignore_table));
+  memset((char *) &ignore_table, 0, sizeof(ignore_table));
 
   exit_code= get_options(&argc, &argv);
   if (exit_code)
 

 
--- mysqlslap.c.orig    2015-07-02 09:59:00.000000000 -0700
+++ mysqlslap.c 2015-07-02 09:59:40.000000000 -0700
@@ -451,7 +451,7 @@
   head_sptr= (stats *)my_malloc(sizeof(stats) * iterations, 
                                 MYF(MY_ZEROFILL|MY_FAE|MY_WME));
 
-  bzero(&conclusion, sizeof(conclusions));
+  memset(&conclusion, 0, sizeof(conclusions));
 
   if (auto_actual_queries)
     client_limit= auto_actual_queries;
 

 
--- mysqltest.cc.orig   2015-07-02 10:00:43.000000000 -0700
+++ mysqltest.cc        2015-07-02 10:04:54.000000000 -0700
@@ -641,7 +641,7 @@
   size_t m_bytes_written;
 public:
   LogFile() : m_file(NULL), m_bytes_written(0) {
-    bzero(m_file_name, sizeof(m_file_name));
+    memset(m_file_name, 0, sizeof(m_file_name));
   }
 
   ~LogFile() {
@@ -9842,8 +9842,8 @@
 
   free_replace();
 
-  bzero((char*) &to_array,sizeof(to_array));
-  bzero((char*) &from_array,sizeof(from_array));
+  memset((char *) &to_array_sizeof(to_array));
+  memset((char *) &from_array, 0, sizeof(from_array));
   if (!*from)
     die("Missing argument in %s", command->query);
   start= buff= (char*)my_malloc(strlen(from)+1,MYF(MY_WME | MY_FAE));
@@ -10026,7 +10026,7 @@
   /* for each regexp substitution statement */
   while (p < expr_end)
   {
-    bzero(&reg,sizeof(reg));
+    memset(&reg, 0, sizeof(reg));
     /* find the start of the statement */
     while (my_isspace(charset_info, *p) && p < expr_end)
       p++;
@@ -10473,7 +10473,7 @@
     if (len > max_length)
       max_length=len;
   }
-  bzero((char*) is_word_end,sizeof(is_word_end));
+  memset((char *) is_word_end, 0, sizeof(is_word_end));
   for (i=0 ; word_end_chars[i] ; i++)
     is_word_end[(uchar) word_end_chars[i]]=1;
 
@@ -10564,7 +10564,7 @@
       or_bits(sets.set+used_sets,sets.set);    /* Can restart from start */
 
     /* Find all chars that follows current sets */
-    bzero((char*) used_chars,sizeof(used_chars));
+    memset((char *) used_chars, 0, sizeof(used_chars));
     for (i= (uint) ~0; (i=get_next_bit(sets.set+used_sets,i)) ;)
     {
       used_chars[follow[i].chr]=1;
@@ -10698,7 +10698,7 @@
 
 int init_sets(REP_SETS *sets,uint states)
 {
-  bzero((char*) sets,sizeof(*sets));
+  memset((char *) sets, 0, sizeof(*sets));
   sets->size_of_bits=((states+7)/8);
   if (!(sets->set_buffer=(REP_SET*) my_malloc(sizeof(REP_SET)*SET_MALLOC_HUNC,
                                              MYF(MY_WME))))
@@ -10729,8 +10729,8 @@
   {
     sets->extra--;
     set=sets->set+ sets->count++;
-    bzero((char*) set->bits,sizeof(uint)*sets->size_of_bits);
-    bzero((char*) &set->next[0],sizeof(set->next[0])*LAST_CHAR_CODE);
+    memset((char *) set->bits, 0, sizeof(uint)*sets->size_of_bits);
+    memset((char *) &set->next[0], 0, sizeof(set->next[0])*LAST_CHAR_CODE);
     set->found_offset=0;
     set->found_len=0;
     set->table_offset= (uint) ~0;
	 

 
In directory 'mariadb-10.0.2x/dbug', file 'dbug.c', there are several
instances of calls to bzero() (which is deprecated), the patch file
below replaces the calls to bzero() with memset().
 
--- dbug.c.orig 2015-07-02 10:33:47.000000000 -0700
+++ dbug.c      2015-07-02 10:36:27.000000000 -0700
@@ -342,7 +342,7 @@
     sstdout->file= stdout;
     sstderr->file= stderr;
     pthread_mutex_init(&THR_LOCK_dbug, NULL);
-    bzero(&init_settings, sizeof(init_settings));
+    memset(&init_settings, 0, sizeof(init_settings));
     init_settings.out_file= sstderr;
     init_settings.flags=OPEN_APPEND;
   }
@@ -352,7 +352,7 @@
   if (!(cs= *cs_ptr))
   {
     cs=(CODE_STATE*) DbugMalloc(sizeof(*cs));
-    bzero((uchar*) cs,sizeof(*cs));
+    memset((uchar *) cs, 0, sizeof(*cs));
     cs->process= db_process ? db_process : "dbug";
     cs->func= "?func";
     cs->file= "?file";
@@ -865,7 +865,7 @@
 void _db_set_init_(const char *control)
 {
   CODE_STATE tmp_cs;
-  bzero((uchar*) &tmp_cs, sizeof(tmp_cs));
+  memset((uchar *) &tmp_cs, 0, sizeof(tmp_cs));
   tmp_cs.stack= &init_settings;
   tmp_cs.process= db_process ? db_process : "dbug";
   DbugParse(&tmp_cs, control);
@@ -1054,7 +1054,7 @@
 int _db_explain_init_(char *buf, size_t len)
 {
   CODE_STATE cs;
-  bzero((uchar*) &cs,sizeof(cs));
+  memset((uchar *) &cs, 0, sizeof(cs));
   cs.stack=&init_settings;
   return _db_explain_(&cs, buf, len);
 }
@@ -1559,7 +1559,7 @@
   struct settings *new_malloc;
 
   new_malloc= (struct settings *) DbugMalloc(sizeof(struct settings));
-  bzero(new_malloc, sizeof(*new_malloc));
+  memset(new_malloc, 0, sizeof(*new_malloc));
   new_malloc->next= cs->stack;
   cs->stack= new_malloc;
 }
@@ -1637,7 +1637,7 @@
   else
   {
     cs= &dummy_cs;
-    bzero(cs, sizeof(*cs));
+    memset(cs, 0, sizeof(*cs));
   }
 
   cs->stack= &init_settings;
   

Questions, Comments, Suggestions?

I am attaching the patch file(s) to this bug report.

Bill Parker (wp02855 at gmail dot com)



 Comments   
Comment by Sergei Golubchik [ 2015-07-24 ]

I personally prefer bzero() — I think it has clearer semantics making this special case (setting to 0) easier to spot.

But as it's deprecated, I'd rather fix it in one of the headers (e.g. in my_global.h), like

#define bzero(A,B) memset((A), 0, (B))

or

static inline void bzero(void *s, size_t n) { memset(s, 0, n); }

Instead of correcting all invocation of bzero() everywhere in the sources.

Comment by Bill Parker [ 2015-07-24 ]

That also works, though going forward, memset() should be used (at least
that is what most coding references say)...

On Fri, Jul 24, 2015 at 1:44 PM, Sergei Golubchik (JIRA) <

Comment by Daniel Black [ 2018-02-11 ]

Define of bzero to memset committed in 0300935ff3f6b422cff4c383d74ce0124dfd7f1f

Generated at Thu Feb 08 07:26:58 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.