vppinfra: make _vec_len() read-only

Use of _vec_len() to set vector length breaks address sanitizer.
Users should use vec_set_len(), vec_inc_len(), vec_dec_len () instead.

Type: improvement
Change-Id: I441ae948771eb21c23a61f3ff9163bdad74a2cb8
Signed-off-by: Damjan Marion <damarion@cisco.com>
diff --git a/src/vlib/cli.c b/src/vlib/cli.c
index b7c4c00..dcabe8d 100644
--- a/src/vlib/cli.c
+++ b/src/vlib/cli.c
@@ -617,7 +617,7 @@
 		      vec_add1 (c_error->what, 0);
 		      ed->err = elog_string (vlib_get_elog_main (),
 					     (char *) c_error->what);
-		      _vec_len (c_error->what) -= 1;
+		      vec_dec_len (c_error->what, 1);
 		    }
 		  else
 		    ed->err = elog_string (vlib_get_elog_main (), "OK");
@@ -1179,7 +1179,7 @@
 
   /* Remove any extra space at end. */
   if (l > 0 && s[l - 1] == ' ')
-    _vec_len (s) -= 1;
+    vec_dec_len (s, 1);
 
   *result = s;
   return index_of_last_space;
diff --git a/src/vlib/lex.c b/src/vlib/lex.c
index 1cc8f16..7facba5 100644
--- a/src/vlib/lex.c
+++ b/src/vlib/lex.c
@@ -113,7 +113,7 @@
 				lm->lex_token_names[VLIB_LEX_word],
 				rv->value.as_pointer);
 	      }
-	    _vec_len (lm->token_buffer) = 0;
+	    vec_set_len (lm->token_buffer, 0);
 
 	    /* Rescan the character which terminated the keyword/word. */
 	    lm->current_index--;
@@ -233,7 +233,7 @@
 vlib_lex_reset (vlib_lex_main_t * lm, u8 * input_vector)
 {
   if (lm->pushback_vector)
-    _vec_len (lm->pushback_vector) = 0;
+    vec_set_len (lm->pushback_vector, 0);
   lm->pushback_sp = -1;
 
   lm->input_vector = input_vector;
@@ -255,7 +255,7 @@
 #undef _
 
   vec_validate (lm->token_buffer, 127);
-  _vec_len (lm->token_buffer) = 0;
+  vec_set_len (lm->token_buffer, 0);
 
   return 0;
 }
diff --git a/src/vlib/main.c b/src/vlib/main.c
index fd0c1fc..a9efe7e 100644
--- a/src/vlib/main.c
+++ b/src/vlib/main.c
@@ -81,7 +81,7 @@
     {
       /* Allocate from end of free list. */
       f = fs->free_frames[l - 1];
-      _vec_len (fs->free_frames) = l - 1;
+      vec_set_len (fs->free_frames, l - 1);
     }
   else
     {
@@ -1458,7 +1458,7 @@
   if (is_main)
     {
       vec_resize (nm->pending_frames, 32);
-      _vec_len (nm->pending_frames) = 0;
+      vec_set_len (nm->pending_frames, 0);
     }
 
   /* Mark time of main loop start. */
@@ -1584,7 +1584,7 @@
       for (i = 0; i < _vec_len (nm->pending_frames); i++)
 	cpu_time_now = dispatch_pending_node (vm, i, cpu_time_now);
       /* Reset pending vector for next iteration. */
-      _vec_len (nm->pending_frames) = 0;
+      vec_set_len (nm->pending_frames, 0);
 
       if (is_main)
 	{
@@ -1670,7 +1670,7 @@
 			dispatch_suspended_process (vm, di, cpu_time_now);
 		    }
 		}
-	      _vec_len (nm->data_from_advancing_timing_wheel) = 0;
+	      vec_set_len (nm->data_from_advancing_timing_wheel, 0);
 	    }
 	}
       vlib_increment_main_loop_counter (vm);
@@ -1946,7 +1946,7 @@
 					     CLIB_CACHE_LINE_BYTES);
 
   vec_validate (nm->data_from_advancing_timing_wheel, 10);
-  _vec_len (nm->data_from_advancing_timing_wheel) = 0;
+  vec_set_len (nm->data_from_advancing_timing_wheel, 0);
 
   /* Create the process timing wheel */
   TW (tw_timer_wheel_init) ((TWT (tw_timer_wheel) *) nm->timing_wheel,
@@ -1955,9 +1955,9 @@
 			    ~0 /* max expirations per call */ );
 
   vec_validate (vm->pending_rpc_requests, 0);
-  _vec_len (vm->pending_rpc_requests) = 0;
+  vec_set_len (vm->pending_rpc_requests, 0);
   vec_validate (vm->processing_rpc_requests, 0);
-  _vec_len (vm->processing_rpc_requests) = 0;
+  vec_set_len (vm->processing_rpc_requests, 0);
 
   /* Default params for the buffer allocator fault injector, if configured */
   if (VLIB_BUFFER_ALLOC_FAULT_INJECTOR > 0)
diff --git a/src/vlib/node_funcs.h b/src/vlib/node_funcs.h
index de6fd48..df8ec7c 100644
--- a/src/vlib/node_funcs.h
+++ b/src/vlib/node_funcs.h
@@ -604,7 +604,7 @@
   l = _vec_len (p->pending_event_data_by_type_index[t]);
   if (data_vector)
     vec_add (*data_vector, p->pending_event_data_by_type_index[t], l);
-  _vec_len (p->pending_event_data_by_type_index[t]) = 0;
+  vec_set_len (p->pending_event_data_by_type_index[t], 0);
 
   et = pool_elt_at_index (p->event_type_pool, t);
 
@@ -628,7 +628,7 @@
   l = _vec_len (p->pending_event_data_by_type_index[t]);
   if (data_vector)
     vec_add (*data_vector, p->pending_event_data_by_type_index[t], l);
-  _vec_len (p->pending_event_data_by_type_index[t]) = 0;
+  vec_set_len (p->pending_event_data_by_type_index[t], 0);
 
   vlib_process_maybe_free_event_type (p, t);
 
diff --git a/src/vlib/threads.c b/src/vlib/threads.c
index 567ba62..36a8080 100644
--- a/src/vlib/threads.c
+++ b/src/vlib/threads.c
@@ -235,7 +235,7 @@
 
   /* Set up thread 0 */
   vec_validate_aligned (vlib_worker_threads, 0, CLIB_CACHE_LINE_BYTES);
-  _vec_len (vlib_worker_threads) = 1;
+  vec_set_len (vlib_worker_threads, 1);
   w = vlib_worker_threads;
   w->thread_mheap = clib_mem_get_heap ();
   w->thread_stack = vlib_thread_stacks[0];
@@ -558,7 +558,7 @@
 
   vec_validate_aligned (vgm->vlib_mains, n_vlib_mains - 1,
 			CLIB_CACHE_LINE_BYTES);
-  _vec_len (vgm->vlib_mains) = 0;
+  vec_set_len (vgm->vlib_mains, 0);
   vec_add1_aligned (vgm->vlib_mains, vm, CLIB_CACHE_LINE_BYTES);
 
   if (n_vlib_mains > 1)
@@ -640,7 +640,7 @@
 	      vm_clone->thread_index = worker_thread_index;
 	      vm_clone->pending_rpc_requests = 0;
 	      vec_validate (vm_clone->pending_rpc_requests, 0);
-	      _vec_len (vm_clone->pending_rpc_requests) = 0;
+	      vec_set_len (vm_clone->pending_rpc_requests, 0);
 	      clib_memset (&vm_clone->random_buffer, 0,
 			   sizeof (vm_clone->random_buffer));
 	      clib_spinlock_init
@@ -670,7 +670,7 @@
 	      /* fork the frame dispatch queue */
 	      nm_clone->pending_frames = 0;
 	      vec_validate (nm_clone->pending_frames, 10);
-	      _vec_len (nm_clone->pending_frames) = 0;
+	      vec_set_len (nm_clone->pending_frames, 0);
 
 	      /* fork nodes */
 	      nm_clone->nodes = 0;
@@ -1616,7 +1616,7 @@
   fqm->frame_queue_nelts = frame_queue_nelts;
 
   vec_validate (fqm->vlib_frame_queues, tm->n_vlib_mains - 1);
-  _vec_len (fqm->vlib_frame_queues) = 0;
+  vec_set_len (fqm->vlib_frame_queues, 0);
   for (i = 0; i < tm->n_vlib_mains; i++)
     {
       fq = vlib_frame_queue_alloc (frame_queue_nelts);
diff --git a/src/vlib/trace.c b/src/vlib/trace.c
index 4bbd950..49b521e 100644
--- a/src/vlib/trace.c
+++ b/src/vlib/trace.c
@@ -272,7 +272,7 @@
   for (index = 0; index < vec_len (traces_to_remove); index++)
     {
       trace_index = traces_to_remove[index] - tm->trace_buffer_pool;
-      _vec_len (tm->trace_buffer_pool[trace_index]) = 0;
+      vec_set_len (tm->trace_buffer_pool[trace_index], 0);
       pool_put_index (tm->trace_buffer_pool, trace_index);
     }
 
diff --git a/src/vlib/trace_funcs.h b/src/vlib/trace_funcs.h
index 9313d41..3ed4768 100644
--- a/src/vlib/trace_funcs.h
+++ b/src/vlib/trace_funcs.h
@@ -125,7 +125,7 @@
   vlib_trace_main_t *tm = &vm->trace_main;
   u32 trace_index = vlib_buffer_get_trace_index (b);
   vlib_validate_trace (tm, b);
-  _vec_len (tm->trace_buffer_pool[trace_index]) = 0;
+  vec_set_len (tm->trace_buffer_pool[trace_index], 0);
   pool_put_index (tm->trace_buffer_pool, trace_index);
 }
 
diff --git a/src/vlib/unix/cli.c b/src/vlib/unix/cli.c
index c546948..244c8df 100644
--- a/src/vlib/unix/cli.c
+++ b/src/vlib/unix/cli.c
@@ -1610,7 +1610,7 @@
 
       /* Delete the desired text from the command */
       memmove (cf->current_command, cf->current_command + j, delta);
-      _vec_len (cf->current_command) = delta;
+      vec_set_len (cf->current_command, delta);
 
       /* Print the new contents */
       unix_vlib_cli_output_cooked (cf, uf, cf->current_command, delta);
@@ -1635,7 +1635,7 @@
 	unix_vlib_cli_output_cursor_left (cf, uf);
 
       /* Truncate the line at the cursor */
-      _vec_len (cf->current_command) = cf->cursor;
+      vec_set_len (cf->current_command, cf->cursor);
 
       cf->search_mode = 0;
       break;
@@ -1677,7 +1677,7 @@
 		unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
 	      for (; (cf->current_command + cf->cursor) > save; cf->cursor--)
 		unix_vlib_cli_output_cursor_left (cf, uf);
-	      _vec_len (cf->current_command) -= delta;
+	      vec_dec_len (cf->current_command, delta);
 	    }
 	}
       cf->search_mode = 0;
@@ -1734,13 +1734,13 @@
 	  if (cf->excursion == vec_len (cf->command_history))
 	    {
 	      /* down-arrowed to last entry - want a blank line */
-	      _vec_len (cf->current_command) = 0;
+	      vec_set_len (cf->current_command, 0);
 	    }
 	  else if (cf->excursion < 0)
 	    {
 	      /* up-arrowed over the start to the end, want a blank line */
 	      cf->excursion = vec_len (cf->command_history);
-	      _vec_len (cf->current_command) = 0;
+	      vec_set_len (cf->current_command, 0);
 	    }
 	  else
 	    {
@@ -1753,7 +1753,7 @@
 	      vec_validate (cf->current_command, vec_len (prev) - 1);
 
 	      clib_memcpy (cf->current_command, prev, vec_len (prev));
-	      _vec_len (cf->current_command) = vec_len (prev);
+	      vec_set_len (cf->current_command, vec_len (prev));
 	      unix_vlib_cli_output_cooked (cf, uf, cf->current_command,
 					   vec_len (cf->current_command));
 	    }
@@ -1840,7 +1840,7 @@
 	      cf->cursor++;
 	      unix_vlib_cli_output_cursor_left (cf, uf);
 	      cf->cursor--;
-	      _vec_len (cf->current_command)--;
+	      vec_dec_len (cf->current_command, 1);
 	    }
 	  else if (cf->cursor > 0)
 	    {
@@ -1848,7 +1848,7 @@
 	      j = vec_len (cf->current_command) - cf->cursor;
 	      memmove (cf->current_command + cf->cursor - 1,
 		       cf->current_command + cf->cursor, j);
-	      _vec_len (cf->current_command)--;
+	      vec_dec_len (cf->current_command, 1);
 
 	      /* redraw the rest of the line */
 	      unix_vlib_cli_output_cursor_left (cf, uf);
@@ -1884,7 +1884,7 @@
 	      j = vec_len (cf->current_command) - cf->cursor - 1;
 	      memmove (cf->current_command + cf->cursor,
 		       cf->current_command + cf->cursor + 1, j);
-	      _vec_len (cf->current_command)--;
+	      vec_dec_len (cf->current_command, 1);
 	      /* redraw the rest of the line */
 	      unix_vlib_cli_output_cooked (cf, uf,
 					   cf->current_command + cf->cursor,
@@ -1956,7 +1956,7 @@
 	      vec_resize (save, vec_len (cf->current_command) - cf->cursor);
 	      clib_memcpy (save, cf->current_command + cf->cursor,
 			   vec_len (cf->current_command) - cf->cursor);
-	      _vec_len (cf->current_command) = cf->cursor;
+	      vec_set_len (cf->current_command, cf->cursor);
 	    }
 	  else
 	    {
@@ -1978,7 +1978,7 @@
 	      cf->cursor--;
 	      j--;
 	    }
-	  _vec_len (cf->current_command) = j;
+	  vec_set_len (cf->current_command, j);
 
 	  /* replace it with the newly expanded command */
 	  vec_append (cf->current_command, completed);
@@ -2385,7 +2385,7 @@
 
 	      vec_validate (cf->current_command, vec_len (item) - 1);
 	      clib_memcpy (cf->current_command, item, vec_len (item));
-	      _vec_len (cf->current_command) = vec_len (item);
+	      vec_set_len (cf->current_command, vec_len (item));
 
 	      unix_vlib_cli_output_cooked (cf, uf, cf->current_command,
 					   vec_len (cf->current_command));
@@ -2599,7 +2599,7 @@
 					 0 /* level */ ,
 					 8 /* max_level */ );
       /* Macro processor NULL terminates the return */
-      _vec_len (expanded) -= 1;
+      vec_dec_len (expanded, 1);
       vec_reset_length (cf->current_command);
       vec_append (cf->current_command, expanded);
       vec_free (expanded);
@@ -2754,7 +2754,7 @@
 	}
 
       if (data)
-	_vec_len (data) = 0;
+	vec_set_len (data, 0);
     }
 
 done:
@@ -2836,7 +2836,7 @@
 	return clib_error_return_unix (0, "read");
 
       n_read = n < 0 ? 0 : n;
-      _vec_len (cf->input_vector) = l + n_read;
+      vec_set_len (cf->input_vector, l + n_read);
     }
 
   if (!(n < 0))
@@ -2918,7 +2918,7 @@
       vec_free (old_name);
 
       vlib_node_set_state (vm, n->index, VLIB_NODE_STATE_POLLING);
-      _vec_len (cm->unused_cli_process_node_indices) = l - 1;
+      vec_set_len (cm->unused_cli_process_node_indices, l - 1);
     }
   else
     {
@@ -2954,7 +2954,7 @@
   cf->output_vector = 0;
   cf->input_vector = 0;
   vec_validate (cf->current_command, 0);
-  _vec_len (cf->current_command) = 0;
+  vec_set_len (cf->current_command, 0);
 
   vlib_start_process (vm, n->runtime_index);
 
@@ -3445,7 +3445,7 @@
 					 0 /* level */ ,
 					 8 /* max_level */ );
       /* Macro processor NULL terminates the return */
-      _vec_len (expanded) -= 1;
+      vec_dec_len (expanded, 1);
       vec_reset_length (sub_input.buffer);
       vec_append (sub_input.buffer, expanded);
       vec_free (expanded);
diff --git a/src/vlib/unix/main.c b/src/vlib/unix/main.c
index 0b73597..3710d8e 100644
--- a/src/vlib/unix/main.c
+++ b/src/vlib/unix/main.c
@@ -316,7 +316,7 @@
 	  n = read (fd, buf + l, 4096);
 	  if (n > 0)
 	    {
-	      _vec_len (buf) = l + n;
+	      vec_set_len (buf, l + n);
 	      if (n < 4096)
 		break;
 	    }
diff --git a/src/vlib/unix/mc_socket.c b/src/vlib/unix/mc_socket.c
index 9800b1e..1f3b4e9 100644
--- a/src/vlib/unix/mc_socket.c
+++ b/src/vlib/unix/mc_socket.c
@@ -90,7 +90,7 @@
   h.msg_namelen = sizeof (tx_addr[0]);
 
   if (msm->iovecs)
-    _vec_len (msm->iovecs) = 0;
+    vec_set_len (msm->iovecs, 0);
 
   n_bytes = append_buffer_index_to_iovec (vm, buffer_index, &msm->iovecs);
   ASSERT (n_bytes <= msm->mc_main.transport.max_packet_size);
@@ -177,7 +177,7 @@
       vec_validate (msm->rx_buffers, max_alloc - 1);
       n_alloc =
 	vlib_buffer_alloc (vm, msm->rx_buffers + n_left, max_alloc - n_left);
-      _vec_len (msm->rx_buffers) = n_left + n_alloc;
+      vec_set_len (msm->rx_buffers, n_left + n_alloc);
     }
 
   ASSERT (vec_len (msm->rx_buffers) >= n_mtu);
@@ -192,7 +192,7 @@
       msm->iovecs[i].iov_base = b->data;
       msm->iovecs[i].iov_len = buffer_size;
     }
-  _vec_len (msm->iovecs) = n_mtu;
+  vec_set_len (msm->iovecs, n_mtu);
 
   {
     struct msghdr h;
@@ -237,7 +237,7 @@
       b->next_buffer = msm->rx_buffers[i_rx];
     }
 
-  _vec_len (msm->rx_buffers) = i_rx;
+  vec_set_len (msm->rx_buffers, i_rx);
 
   return 0 /* no error */ ;
 }
@@ -418,7 +418,7 @@
 	}
     }
 
-  _vec_len (c->input_vector) = l + n;
+  vec_set_len (c->input_vector, l + n);
 
   if (is_eof && vec_len (c->input_vector) > 0)
     {
@@ -426,7 +426,7 @@
 	{
 	  mc_msg_catchup_request_handler (mcm, (void *) c->input_vector,
 					  c - msm->catchups);
-	  _vec_len (c->input_vector) = 0;
+	  vec_set_len (c->input_vector, 0);
 	}
       else
 	{
diff --git a/src/vlib/unix/util.c b/src/vlib/unix/util.c
index 03aef36..04cd6f5 100644
--- a/src/vlib/unix/util.c
+++ b/src/vlib/unix/util.c
@@ -86,8 +86,8 @@
       s = format (s, "%s/%s", dir_name, e->d_name);
       t = format (t, "%s", e->d_name);
       error = f (arg, s, t);
-      _vec_len (s) = 0;
-      _vec_len (t) = 0;
+      vec_set_len (s, 0);
+      vec_set_len (t, 0);
 
       if (error)
 	break;
@@ -116,7 +116,7 @@
 	      error = clib_error_return_unix (0, "mkdir '%s'", c);
 	      goto done;
 	    }
-	  _vec_len (c)--;
+	  vec_dec_len (c, 1);
 	}
       vec_add1 (c, path[i]);
       i++;