Robust sizeof() usage at function parameter and use memcpy destination sizeof...

...for more resilient to errors.

Signed-off-by: Andreas Mohr <and@gmx.li>
Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
This commit is contained in:
Andreas Mohr 2016-01-01 11:23:17 +03:00 committed by Andrew Borodin
parent 0e05710715
commit 697e406eb8
22 changed files with 64 additions and 59 deletions

View File

@ -923,8 +923,10 @@ get_modifier (void)
{
int shift_ext_status;
if (devctl (fileno (stdin), DCMD_CHR_LINESTATUS, &mod_status, sizeof (int), NULL) == -1)
if (devctl (fileno (stdin), DCMD_CHR_LINESTATUS, &mod_status, sizeof (mod_status), NULL) ==
-1)
return 0;
shift_ext_status = mod_status & 0xffffff00UL;
mod_status &= 0x7f;
if (mod_status & _LINESTATUS_CON_ALT)

View File

@ -798,7 +798,7 @@ close_error_pipe (int error, const char *text)
return 1;
}
close (old_error);
len = read (error_pipe[0], msg, MAX_PIPE_SIZE - 1);
len = read (error_pipe[0], msg, sizeof (msg) - 1);
if (len >= 0)
msg[len] = 0;
@ -1132,7 +1132,7 @@ mc_realpath (const char *path, char *resolved_path)
}
else
{
g_snprintf (got_path, PATH_MAX, "%s", new_path);
g_snprintf (got_path, sizeof (got_path), "%s", new_path);
g_free (new_path);
new_path = got_path;
}

View File

@ -399,7 +399,7 @@ fetch_hosts (const char *filename)
if (!file)
return;
while (fgets (buffer, 255, file) != NULL)
while (fgets (buffer, sizeof (buffer) - 1, file) != NULL)
{
/* Skip to first character. */
for (bi = buffer; bi[0] != '\0' && str_isspace (bi); str_next_char (&bi));
@ -1284,7 +1284,7 @@ try_complete (char *text, int *lc_start, int *lc_end, input_complete_t flags)
try_complete_automation_state_t state;
char **matches = NULL;
memset (&state, 0, sizeof (try_complete_automation_state_t));
memset (&state, 0, sizeof (state));
state.flags = flags;
SHOW_C_CTX ("try_complete");

View File

@ -256,7 +256,7 @@ background_attention (int fd, void *closure)
if (have_ctx)
{
if (read (fd, ctx, sizeof (file_op_context_t)) != sizeof (file_op_context_t))
if (read (fd, ctx, sizeof (*ctx)) != sizeof (*ctx))
{
return reading_failed (-1, data);
}
@ -342,9 +342,9 @@ background_attention (int fd, void *closure)
}
/* Send the result code and the value for shared variables */
ret = write (to_child_fd, &result, sizeof (int));
ret = write (to_child_fd, &result, sizeof (result));
if (have_ctx && to_child_fd != -1)
ret = write (to_child_fd, ctx, sizeof (file_op_context_t));
ret = write (to_child_fd, ctx, sizeof (*ctx));
}
else if (type == Return_String)
{
@ -416,12 +416,12 @@ parent_call_header (void *routine, int argc, enum ReturnType type, file_op_conte
have_ctx = (ctx != NULL);
ret = write (parent_fd, &routine, sizeof (routine));
ret = write (parent_fd, &argc, sizeof (int));
ret = write (parent_fd, &argc, sizeof (argc));
ret = write (parent_fd, &type, sizeof (type));
ret = write (parent_fd, &have_ctx, sizeof (have_ctx));
if (have_ctx)
ret = write (parent_fd, ctx, sizeof (file_op_context_t));
ret = write (parent_fd, ctx, sizeof (*ctx));
(void) ret;
}
@ -442,13 +442,13 @@ parent_va_call (void *routine, gpointer data, int argc, va_list ap)
len = va_arg (ap, int);
value = va_arg (ap, void *);
ret = write (parent_fd, &len, sizeof (int));
ret = write (parent_fd, &len, sizeof (len));
ret = write (parent_fd, value, len);
}
ret = read (from_parent_fd, &i, sizeof (int));
ret = read (from_parent_fd, &i, sizeof (i));
if (ctx)
ret = read (from_parent_fd, ctx, sizeof (file_op_context_t));
ret = read (from_parent_fd, ctx, sizeof (*ctx));
(void) ret;
return i;
@ -470,14 +470,14 @@ parent_va_call_string (void *routine, int argc, va_list ap)
len = va_arg (ap, int);
value = va_arg (ap, void *);
if ((write (parent_fd, &len, sizeof (int)) != sizeof (int)) ||
if ((write (parent_fd, &len, sizeof (len)) != sizeof (len)) ||
(write (parent_fd, value, len) != len))
{
return NULL;
}
}
if (read (from_parent_fd, &i, sizeof (int)) != sizeof (int))
if (read (from_parent_fd, &i, sizeof (i)) != sizeof (i))
return NULL;
if (!i)
return NULL;

View File

@ -1065,7 +1065,7 @@ lcsubstr (const char *s, int m, const char *t, int n, GArray * ret, int min)
Lprev = Lcurr;
Lcurr = L;
#ifdef USE_MEMSET_IN_LCS
memset (Lcurr, 0, (n + 1) * sizeof (int));
memset (Lcurr, 0, (n + 1) * sizeof (*Lcurr));
#endif
for (j = 0; j < n; j++)
{

View File

@ -2238,7 +2238,7 @@ edit_reload_line (WEdit * edit, const vfs_path_t * filename_vpath, long line)
}
edit_clean (edit);
memcpy (edit, e, sizeof (WEdit));
memcpy (edit, e, sizeof (*edit));
g_free (e);
return TRUE;

View File

@ -554,7 +554,7 @@ edit_draw_this_line (WEdit * edit, off_t b, long row, long start_col, long end_c
cur_line = edit->start_line + row;
if (cur_line <= (unsigned int) edit->buffer.lines)
{
g_snprintf (line_stat, LINE_STATE_WIDTH + 1, "%7i ", cur_line + 1);
g_snprintf (line_stat, sizeof (line_stat), "%7i ", cur_line + 1);
}
else
{

View File

@ -247,10 +247,12 @@ compare_files (const vfs_path_t * vpath1, const vfs_path_t * vpath2, off_t size)
rotate_dash (TRUE);
do
{
while ((n1 = read (file1, buf1, BUFSIZ)) == -1 && errno == EINTR);
while ((n2 = read (file2, buf2, BUFSIZ)) == -1 && errno == EINTR);
while ((n1 = read (file1, buf1, sizeof (buf1))) == -1 && errno == EINTR)
;
while ((n2 = read (file2, buf2, sizeof (buf2))) == -1 && errno == EINTR)
;
}
while (n1 == n2 && n1 == BUFSIZ && !memcmp (buf1, buf2, BUFSIZ));
while (n1 == n2 && n1 == sizeof (buf1) && memcmp (buf1, buf2, sizeof (buf1)) == 0);
result = (n1 != n2) || memcmp (buf1, buf2, n1);
#endif /* !HAVE_MMAP */
close (file2);

View File

@ -561,7 +561,7 @@ dir_list_init (dir_list * list)
}
fentry = &list->list[0];
memset (fentry, 0, sizeof (file_entry_t));
memset (fentry, 0, sizeof (*fentry));
fentry->fnamelen = 2;
fentry->fname = g_strndup ("..", fentry->fnamelen);
fentry->f.link_to_dir = 0;

View File

@ -909,18 +909,18 @@ file_progress_show (file_op_context_t * ctx, off_t done, off_t total,
file_eta_prepare_for_show (buffer2, ctx->eta_secs, FALSE);
if (ctx->bps == 0)
g_snprintf (buffer, BUF_TINY, "%s %s", buffer2, stalled_msg);
g_snprintf (buffer, sizeof (buffer), "%s %s", buffer2, stalled_msg);
else
{
char buffer3[BUF_TINY];
file_bps_prepare_for_show (buffer3, ctx->bps);
g_snprintf (buffer, BUF_TINY, "%s (%s) %s", buffer2, buffer3, stalled_msg);
g_snprintf (buffer, sizeof (buffer), "%s (%s) %s", buffer2, buffer3, stalled_msg);
}
}
else
{
g_snprintf (buffer, BUF_TINY, "%s", stalled_msg);
g_snprintf (buffer, sizeof (buffer), "%s", stalled_msg);
}
label_set_text (ui->progress_file_label, buffer);
@ -942,9 +942,9 @@ file_progress_show_count (file_op_context_t * ctx, size_t done, size_t total)
return;
if (ctx->progress_totals_computed)
g_snprintf (buffer, BUF_TINY, _("Files processed: %zu/%zu"), done, total);
g_snprintf (buffer, sizeof (buffer), _("Files processed: %zu/%zu"), done, total);
else
g_snprintf (buffer, BUF_TINY, _("Files processed: %zu"), done);
g_snprintf (buffer, sizeof (buffer), _("Files processed: %zu"), done);
label_set_text (ui->total_files_processed_label, buffer);
}
@ -991,22 +991,23 @@ file_progress_show_total (file_op_total_context_t * tctx, file_op_context_t * ct
{
file_eta_prepare_for_show (buffer3, tctx->eta_secs, TRUE);
if (tctx->bps == 0)
g_snprintf (buffer, BUF_TINY, _("Time: %s %s"), buffer2, buffer3);
g_snprintf (buffer, sizeof (buffer), _("Time: %s %s"), buffer2, buffer3);
else
{
file_bps_prepare_for_show (buffer4, (long) tctx->bps);
g_snprintf (buffer, BUF_TINY, _("Time: %s %s (%s)"), buffer2, buffer3, buffer4);
g_snprintf (buffer, sizeof (buffer), _("Time: %s %s (%s)"), buffer2, buffer3,
buffer4);
}
}
else
{
if (tctx->bps == 0)
g_snprintf (buffer, BUF_TINY, _("Time: %s"), buffer2);
g_snprintf (buffer, sizeof (buffer), _("Time: %s"), buffer2);
else
{
file_bps_prepare_for_show (buffer4, (long) tctx->bps);
g_snprintf (buffer, BUF_TINY, _("Time: %s (%s)"), buffer2, buffer4);
g_snprintf (buffer, sizeof (buffer), _("Time: %s (%s)"), buffer2, buffer4);
}
}
@ -1017,11 +1018,11 @@ file_progress_show_total (file_op_total_context_t * tctx, file_op_context_t * ct
{
size_trunc_len (buffer2, 5, tctx->copied_bytes, 0, panels_options.kilobyte_si);
if (!ctx->progress_totals_computed)
g_snprintf (buffer, BUF_TINY, _(" Total: %s "), buffer2);
g_snprintf (buffer, sizeof (buffer), _(" Total: %s "), buffer2);
else
{
size_trunc_len (buffer3, 5, ctx->progress_bytes, 0, panels_options.kilobyte_si);
g_snprintf (buffer, BUF_TINY, _(" Total: %s/%s "), buffer2, buffer3);
g_snprintf (buffer, sizeof (buffer), _(" Total: %s/%s "), buffer2, buffer3);
}
hline_set_text (ui->total_bytes_label, buffer);

View File

@ -1726,7 +1726,7 @@ my_statfs (struct my_statfs *myfs_stats, const char *path)
if (entry != NULL)
{
memset (&fs_use, 0, sizeof (struct fs_usage));
memset (&fs_use, 0, sizeof (fs_use));
get_fs_usage (entry->me_mountdir, NULL, &fs_use);
myfs_stats->type = entry->me_dev;

View File

@ -329,7 +329,7 @@ do_external_panelize (char *command)
while (TRUE)
{
clearerr (external);
if (fgets (line, MC_MAXPATHLEN, external) == NULL)
if (fgets (line, sizeof (line), external) == NULL)
{
if (ferror (external) && errno == EINTR)
continue;

View File

@ -1388,7 +1388,7 @@ panel_load_setup (WPanel * panel, const char *section)
for (i = 0; i < LIST_TYPES; i++)
{
g_free (panel->user_status_format[i]);
g_snprintf (buffer2, BUF_TINY, "user_status%lld", (long long) i);
g_snprintf (buffer2, sizeof (buffer2), "user_status%lld", (long long) i);
panel->user_status_format[i] =
mc_config_get_string (mc_panels_config, section, buffer2, DEFAULT_USER_FORMAT);
}
@ -1424,7 +1424,7 @@ panel_save_setup (WPanel * panel, const char *section)
for (i = 0; i < LIST_TYPES; i++)
{
g_snprintf (buffer, BUF_TINY, "user_status%lld", (long long) i);
g_snprintf (buffer, sizeof (buffer), "user_status%lld", (long long) i);
mc_config_set_string (mc_panels_config, section, buffer, panel->user_status_format[i]);
}

View File

@ -570,7 +570,7 @@ feed_subshell (int how, int fail_on_error)
else if (FD_ISSET (subshell_pipe[READ], &read_set))
/* Read the subshell's CWD and capture its prompt */
{
bytes = read (subshell_pipe[READ], subshell_cwd, MC_MAXPATHLEN + 1);
bytes = read (subshell_pipe[READ], subshell_cwd, sizeof (subshell_cwd));
if (bytes <= 0)
{
tcsetattr (STDOUT_FILENO, TCSANOW, &shell_mode);

View File

@ -832,7 +832,7 @@ ftpfs_open_socket (struct vfs_class *me, struct vfs_s_super *super)
tty_enable_interrupt_key (); /* clear the interrupt flag */
memset (&hints, 0, sizeof (struct addrinfo));
memset (&hints, 0, sizeof (hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
@ -1258,8 +1258,8 @@ ftpfs_init_data_socket (struct vfs_class *me, struct vfs_s_super *super,
{
int result;
memset (data_addr, 0, sizeof (struct sockaddr_storage));
*data_addrlen = sizeof (struct sockaddr_storage);
memset (data_addr, 0, sizeof (*data_addr));
*data_addrlen = sizeof (*data_addr);
if (SUP->use_passive_connection)
result = getpeername (SUP->sock, (struct sockaddr *) data_addr, data_addrlen);

View File

@ -220,7 +220,7 @@ sftpfs_fill_config_entity_from_config (FILE * ssh_config_handler,
while (!feof (ssh_config_handler))
{
char *cr;
if (fgets (buffer, BUF_MEDIUM, ssh_config_handler) == NULL)
if (fgets (buffer, sizeof (buffer), ssh_config_handler) == NULL)
{
if (errno != 0)
{

View File

@ -83,7 +83,7 @@ sftpfs_open_socket (struct vfs_s_super *super, GError ** mcerror)
tty_enable_interrupt_key (); /* clear the interrupt flag */
memset (&hints, 0, sizeof (struct addrinfo));
memset (&hints, 0, sizeof (hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;

View File

@ -675,7 +675,7 @@ sftpfs_cb_fill_names (struct vfs_class *me, fill_names_f func)
void
sftpfs_init_class (void)
{
memset (&sftpfs_class, 0, sizeof (struct vfs_class));
memset (&sftpfs_class, 0, sizeof (sftpfs_class));
sftpfs_class.name = "sftpfs";
sftpfs_class.prefix = "sftp";
sftpfs_class.flags = VFSF_NOLINKS;

View File

@ -176,7 +176,7 @@ sftpfs_cb_dir_load (struct vfs_class *me, struct vfs_s_inode *dir, char *remote_
void
sftpfs_init_subclass (void)
{
memset (&sftpfs_subclass, 0, sizeof (struct vfs_s_subclass));
memset (&sftpfs_subclass, 0, sizeof (sftpfs_subclass));
sftpfs_subclass.flags = VFS_S_REMOTE;
}

View File

@ -354,10 +354,10 @@ tar_get_next_record (struct vfs_s_super *archive, int tard)
(void) archive;
n = mc_read (tard, rec_buf.charptr, RECORDSIZE);
if (n != RECORDSIZE)
n = mc_read (tard, rec_buf.charptr, sizeof (rec_buf.charptr));
if (n != sizeof (rec_buf.charptr))
return NULL; /* An error has occurred */
current_tar_position += RECORDSIZE;
current_tar_position += sizeof (rec_buf.charptr);
return &rec_buf;
}
@ -368,8 +368,8 @@ tar_skip_n_records (struct vfs_s_super *archive, int tard, size_t n)
{
(void) archive;
mc_lseek (tard, n * RECORDSIZE, SEEK_CUR);
current_tar_position += n * RECORDSIZE;
mc_lseek (tard, n * sizeof (rec_buf.charptr), SEEK_CUR);
current_tar_position += n * sizeof (rec_buf.charptr);
}
/* --------------------------------------------------------------------------------------------- */

View File

@ -87,14 +87,14 @@ mcview_ccache_add_entry (coord_cache_t * cache, size_t pos, const coord_cache_en
if (cache->size == cache->capacity)
{
cache->capacity += CACHE_CAPACITY_DELTA;
cache->cache = g_realloc (cache->cache, cache->capacity * sizeof (coord_cache_entry_t *));
cache->cache = g_realloc (cache->cache, cache->capacity * sizeof (*cache->cache));
}
/* insert new entry */
if (pos != cache->size)
memmove (cache->cache[pos + 1], cache->cache[pos],
(cache->size - pos) * sizeof (coord_cache_entry_t *));
cache->cache[pos] = g_memdup (entry, sizeof (coord_cache_entry_t));
(cache->size - pos) * sizeof (*cache->cache));
cache->cache[pos] = g_memdup (entry, sizeof (*entry));
cache->size++;
}
@ -181,7 +181,7 @@ coord_cache_new (void)
cache = g_new (coord_cache_t, 1);
cache->size = 0;
cache->capacity = CACHE_CAPACITY_DELTA;
cache->cache = g_malloc0 (cache->capacity * sizeof (coord_cache_entry_t *));
cache->cache = g_malloc0 (cache->capacity * sizeof (*cache->cache));
return cache;
}

View File

@ -63,7 +63,7 @@ sigaction (int signum, const struct sigaction *act, struct sigaction *oldact)
/* store signum */
tmp_signum = g_new (int, 1);
memcpy (tmp_signum, &signum, sizeof (int));
memcpy (tmp_signum, &signum, sizeof (*tmp_signum));
if (sigaction_signum__captured != NULL)
g_ptr_array_add (sigaction_signum__captured, tmp_signum);
@ -71,7 +71,7 @@ sigaction (int signum, const struct sigaction *act, struct sigaction *oldact)
if (act != NULL)
{
tmp_act = g_new (struct sigaction, 1);
memcpy (tmp_act, act, sizeof (struct sigaction));
memcpy (tmp_act, act, sizeof (*tmp_act));
}
else
tmp_act = NULL;
@ -82,7 +82,7 @@ sigaction (int signum, const struct sigaction *act, struct sigaction *oldact)
if (oldact != NULL)
{
tmp_act = g_new (struct sigaction, 1);
memcpy (tmp_act, oldact, sizeof (struct sigaction));
memcpy (tmp_act, oldact, sizeof (*tmp_act));
}
else
tmp_act = NULL;
@ -131,14 +131,14 @@ signal (int signum, sighandler_t handler)
/* store signum */
tmp_signum = g_new (int, 1);
memcpy (tmp_signum, &signum, sizeof (int));
memcpy (tmp_signum, &signum, sizeof (*tmp_signum));
g_ptr_array_add (signal_signum__captured, tmp_signum);
/* store handler */
if (handler != SIG_DFL)
{
tmp_handler = g_new (sighandler_t, 1);
memcpy (tmp_handler, handler, sizeof (sighandler_t));
memcpy (tmp_handler, handler, sizeof (*tmp_handler));
}
else
tmp_handler = (void *) SIG_DFL;