Use g_strlcpy() instead of strncpy() everywhere

except where I didn't understand the code after 10 seconds of
looking..
This commit is contained in:
Michael Natterer 2019-08-14 23:52:38 +02:00
parent 31bee77be9
commit b007269729
21 changed files with 81 additions and 135 deletions

View file

@ -263,8 +263,7 @@ gimp_text_get_xlfd_field (const gchar *fontname,
if (*t1 == '*')
return NULL;
strncpy (buffer, t1, len);
buffer[len] = 0;
g_strlcpy (buffer, t1, len);
/* Convert to lower case. */
for (p = buffer; *p; p++)

View file

@ -584,8 +584,7 @@ gimp_metadata_deserialize_start_element (GMarkupParseContext *context,
return;
}
strncpy (parse_data->name, name, sizeof (parse_data->name));
parse_data->name[sizeof (parse_data->name) - 1] = 0;
g_strlcpy (parse_data->name, name, sizeof (parse_data->name));
parse_data->base64 = (encoding && ! strcmp (encoding, "base64"));
}

View file

@ -70,7 +70,7 @@ _br_find_exe (GimpBinrelocInitError *error)
return NULL;
}
strncpy (path2, "/proc/self/exe", buf_size - 1);
g_strlcpy (path2, "/proc/self/exe", buf_size);
while (1)
{
@ -106,7 +106,7 @@ _br_find_exe (GimpBinrelocInitError *error)
}
/* path is a symlink. Continue loop and resolve this. */
strncpy (path, path2, buf_size - 1);
g_strlcpy (path, path2, buf_size);
}

View file

@ -2158,8 +2158,8 @@ CML_save_to_file_response (GtkWidget *dialog,
g_message (_("Parameters were saved to '%s'"),
gimp_filename_to_utf8 (filename));
strncpy (VALS.last_file_name, filename,
sizeof (VALS.last_file_name) - 1);
g_strlcpy (VALS.last_file_name, filename,
sizeof (VALS.last_file_name));
g_free (filename);
@ -2391,8 +2391,8 @@ CML_load_parameter_file (const gchar *filename,
sizeof (CML_PARAM));
}
strncpy (VALS.last_file_name, filename,
sizeof (VALS.last_file_name) - 1);
g_strlcpy (VALS.last_file_name, filename,
sizeof (VALS.last_file_name));
}
return flag;
}

View file

@ -574,9 +574,8 @@ run (const gchar *name,
composevals.inputs[2].comp.ID = param[4].data.d_int32;
composevals.inputs[3].comp.ID = param[5].data.d_int32;
strncpy (composevals.compose_type, param[6].data.d_string,
sizeof (composevals.compose_type));
composevals.compose_type[sizeof (composevals.compose_type)-1] = '\0';
g_strlcpy (composevals.compose_type, param[6].data.d_string,
sizeof (composevals.compose_type));
for (i = 0; i < MAX_COMPOSE_IMAGES; i++)
{

View file

@ -359,9 +359,8 @@ run (const gchar *name,
}
else
{
strncpy (decovals.extract_type, param[3].data.d_string,
sizeof (decovals.extract_type));
decovals.extract_type[sizeof (decovals.extract_type) - 1] = '\0';
g_strlcpy (decovals.extract_type, param[3].data.d_string,
sizeof (decovals.extract_type));
decovals.as_layers = nparams > 4 ? param[4].data.d_int32 : FALSE;
decovals.use_registration = (strcmp (name, PLUG_IN_PROC_REG) == 0);
@ -917,8 +916,8 @@ decompose_dialog (void)
gtk_widget_destroy (dialog);
if (run)
strncpy (decovals.extract_type, extract[extract_idx].type,
sizeof decovals.extract_type - 1);
g_strlcpy (decovals.extract_type, extract[extract_idx].type,
sizeof decovals.extract_type);
return run;
}

View file

@ -841,8 +841,7 @@ dicom_add_element (GSList *elements,
element->group_word = group_word;
element->element_word = element_word;
strncpy (element->value_rep, value_rep, sizeof (element->value_rep) - 1);
element->value_rep[sizeof (element->value_rep) - 1] = '\0';
g_strlcpy (element->value_rep, value_rep, sizeof (element->value_rep));
element->element_length = element_length;
element->value = value;
@ -1063,8 +1062,7 @@ dicom_get_elements_list (gint32 image_ID)
guint16 element_word = 0;
/* sacrificial buffer */
strncpy (buf, parasites[i], sizeof (buf) - 1);
buf[sizeof (buf) - 1] = '\0';
g_strlcpy (buf, parasites[i], sizeof (buf));
/* buf should now hold a string of the form
* dcm/XXXX-XXXX-AA where XXXX are Hex values for
@ -1107,10 +1105,7 @@ dicom_get_elements_list (gint32 image_ID)
/* and lastly, the VR */
if (ptr1)
{
strncpy (value_rep, ptr1, sizeof (value_rep) - 1);
value_rep[sizeof (value_rep) - 1] = '\0';
}
g_strlcpy (value_rep, ptr1, sizeof (value_rep));
/*
* If all went according to plan, we should be able

View file

@ -220,11 +220,10 @@ gbr_save (GimpProcedure *procedure,
"gimp-brush-name");
if (parasite)
{
strncpy (info.description,
gimp_parasite_data (parasite),
MIN (sizeof (info.description),
gimp_parasite_data_size (parasite)));
info.description[sizeof (info.description) - 1] = '\0';
g_strlcpy (info.description,
gimp_parasite_data (parasite),
MIN (sizeof (info.description),
gimp_parasite_data_size (parasite)));
gimp_parasite_free (parasite);
}
@ -236,10 +235,7 @@ gbr_save (GimpProcedure *procedure,
name[strlen (name) - 4] = '\0';
if (strlen (name))
{
strncpy (info.description, name, sizeof (info.description));
info.description[sizeof (info.description) - 1] = '\0';
}
g_strlcpy (info.description, name, sizeof (info.description));
g_free (name);
}
@ -261,10 +257,9 @@ gbr_save (GimpProcedure *procedure,
case GIMP_RUN_NONINTERACTIVE:
info.spacing = g_value_get_int (gimp_value_array_index (args, 0));
strncpy (info.description,
g_value_get_string (gimp_value_array_index (args, 1)),
sizeof (info.description));
info.description[sizeof (info.description) - 1] = '\0';
g_strlcpy (info.description,
g_value_get_string (gimp_value_array_index (args, 1)),
sizeof (info.description));
break;
default:
@ -389,7 +384,6 @@ static void
entry_callback (GtkWidget *widget,
gpointer data)
{
strncpy (info.description, gtk_entry_get_text (GTK_ENTRY (widget)),
sizeof (info.description));
info.description[sizeof (info.description) - 1] = '\0';
g_strlcpy (info.description, gtk_entry_get_text (GTK_ENTRY (widget)),
sizeof (info.description));
}

View file

@ -378,8 +378,7 @@ load_image (const gchar *filename,
return -1;
}
strncpy (version, (gchar *) buf + 3, 3);
version[3] = '\0';
g_strlcpy (version, (gchar *) buf + 3, 3);
if ((strcmp (version, "87a") != 0) && (strcmp (version, "89a") != 0))
{

View file

@ -312,11 +312,10 @@ gih_save (GimpProcedure *procedure,
"gimp-brush-pipe-name");
if (parasite)
{
strncpy (info.description,
gimp_parasite_data (parasite),
MIN (sizeof (info.description),
gimp_parasite_data_size (parasite)));
info.description[sizeof (info.description) - 1] = '\0';
g_strlcpy (info.description,
gimp_parasite_data (parasite),
MIN (sizeof (info.description),
gimp_parasite_data_size (parasite)));
gimp_parasite_free (parasite);
}
@ -328,10 +327,7 @@ gih_save (GimpProcedure *procedure,
name[strlen (name) - 4] = '\0';
if (strlen (name))
{
strncpy (info.description, name, sizeof (info.description));
info.description[sizeof (info.description) - 1] = '\0';
}
g_strlcpy (info.description, name, sizeof (info.description));
g_free (name);
}
@ -385,10 +381,9 @@ gih_save (GimpProcedure *procedure,
case GIMP_RUN_NONINTERACTIVE:
info.spacing = g_value_get_int (gimp_value_array_index (args, 0));
strncpy (info.description,
g_value_get_string (gimp_value_array_index (args, 1)),
sizeof (info.description));
info.description[sizeof (info.description) - 1] = '\0';
g_strlcpy (info.description,
g_value_get_string (gimp_value_array_index (args, 1)),
sizeof (info.description));
gihparams.cellwidth = g_value_get_int (gimp_value_array_index (args, 2));
gihparams.cellheight = g_value_get_int (gimp_value_array_index (args, 3));
@ -567,11 +562,8 @@ entry_callback (GtkWidget *widget,
gpointer data)
{
if (data == info.description)
{
strncpy (info.description, gtk_entry_get_text (GTK_ENTRY (widget)),
g_strlcpy (info.description, gtk_entry_get_text (GTK_ENTRY (widget)),
sizeof (info.description));
info.description[sizeof (info.description) - 1] = 0;
}
}
static void

View file

@ -742,5 +742,5 @@ static void
entry_changed_callback (GtkEntry *entry,
gchar *string)
{
strncpy (string, gtk_entry_get_text (entry), 255);
g_strlcpy (string, gtk_entry_get_text (entry), 256);
}

View file

@ -185,11 +185,10 @@ pat_save (GimpProcedure *procedure,
"gimp-pattern-name");
if (parasite)
{
strncpy (description,
gimp_parasite_data (parasite),
MIN (sizeof (description),
gimp_parasite_data_size (parasite)));
description[sizeof (description) - 1] = '\0';
g_strlcpy (description,
gimp_parasite_data (parasite),
MIN (sizeof (description),
gimp_parasite_data_size (parasite)));
gimp_parasite_free (parasite);
}
@ -201,10 +200,7 @@ pat_save (GimpProcedure *procedure,
name[strlen (name) - 4] = '\0';
if (strlen (name))
{
strncpy (description, name, sizeof (description));
description[sizeof (description) - 1] = '\0';
}
g_strlcpy (description, name, sizeof (description));
g_free (name);
}
@ -225,10 +221,9 @@ pat_save (GimpProcedure *procedure,
break;
case GIMP_RUN_NONINTERACTIVE:
strncpy (description,
g_value_get_string (gimp_value_array_index (args, 0)),
sizeof (description));
description[sizeof (description) - 1] = '\0';
g_strlcpy (description,
g_value_get_string (gimp_value_array_index (args, 0)),
sizeof (description));
break;
default:
@ -326,11 +321,8 @@ save_dialog (void)
run = (gimp_dialog_run (GIMP_DIALOG (dialog)) == GTK_RESPONSE_OK);
if (run)
{
strncpy (description, gtk_entry_get_text (GTK_ENTRY (entry)),
g_strlcpy (description, gtk_entry_get_text (GTK_ENTRY (entry)),
sizeof (description));
description[sizeof (description) - 1] = '\0';
}
gtk_widget_destroy (dialog);

View file

@ -880,7 +880,7 @@ run (const gchar *name,
plvals.resolution = size / 4;
plvals.width = size;
plvals.height = size;
strncpy (plvals.pages, "1", sizeof (plvals.pages) - 1);
g_strlcpy (plvals.pages, "1", sizeof (plvals.pages));
check_load_vals ();
image_ID = load_image (param[0].data.d_string, &error);
@ -1009,8 +1009,8 @@ run (const gchar *name,
plvals.height = param[2].data.d_int32;
plvals.use_bbox = param[3].data.d_int32;
if (param[4].data.d_string != NULL)
strncpy (plvals.pages, param[4].data.d_string,
sizeof (plvals.pages));
g_strlcpy (plvals.pages, param[4].data.d_string,
sizeof (plvals.pages));
else
plvals.pages[0] = '\0';
plvals.pages[sizeof (plvals.pages) - 1] = '\0';
@ -1316,7 +1316,7 @@ check_load_vals (void)
plvals.height = 2;
plvals.use_bbox = (plvals.use_bbox != 0);
if (plvals.pages[0] == '\0')
strncpy (plvals.pages, "1-99", sizeof (plvals.pages) - 1);
g_strlcpy (plvals.pages, "1-99", sizeof (plvals.pages));
if ((plvals.pnm_type < 4) || (plvals.pnm_type > 7))
plvals.pnm_type = 6;
if ( (plvals.textalpha != 1) && (plvals.textalpha != 2)
@ -1359,8 +1359,7 @@ page_in_list (gchar *list,
if ((list == NULL) || (*list == '\0'))
return 1;
strncpy (tmplist, list, STR_LENGTH);
tmplist[STR_LENGTH-1] = '\0';
g_strlcpy (tmplist, list, STR_LENGTH);
c0 = c1 = tmplist;
while (*c1) /* Remove all whitespace and break on unsupported characters */
@ -3587,8 +3586,7 @@ load_dialog (const gchar *filename)
range = gimp_page_selector_get_selected_range (GIMP_PAGE_SELECTOR (selector));
}
strncpy (plvals.pages, range, sizeof (plvals.pages) - 1);
plvals.pages[strlen (range)] = '\0';
g_strlcpy (plvals.pages, range, sizeof (plvals.pages));
ps_pagemode = gimp_page_selector_get_target (GIMP_PAGE_SELECTOR (selector));
}
@ -3598,7 +3596,7 @@ load_dialog (const gchar *filename)
}
else
{
strncpy (plvals.pages, "1", sizeof (plvals.pages) - 1);
g_strlcpy (plvals.pages, "1", sizeof (plvals.pages));
ps_pagemode = GIMP_PAGE_SELECTOR_TARGET_IMAGES;
}
@ -3611,10 +3609,8 @@ static void
load_pages_entry_callback (GtkWidget *widget,
gpointer data)
{
gsize nelem = sizeof (plvals.pages);
strncpy (plvals.pages, gtk_entry_get_text (GTK_ENTRY (widget)), nelem);
plvals.pages[nelem-1] = '\0';
g_strlcpy (plvals.pages, gtk_entry_get_text (GTK_ENTRY (widget)),
sizeof (plvals.pages));
}

View file

@ -228,7 +228,7 @@ init_prefix (const gchar *filename)
else
len = MAX_PREFIX;
strncpy (xsvals.prefix, prefix, len);
g_strlcpy (xsvals.prefix, prefix, len);
g_free (prefix);
}
@ -255,7 +255,7 @@ run (const gchar *name,
INIT_I18N ();
gegl_init (NULL, NULL);
strncpy (xsvals.comment, "Created with GIMP", MAX_COMMENT);
g_strlcpy (xsvals.comment, "Created with GIMP", MAX_COMMENT);
run_mode = param[0].data.d_int32;
@ -333,11 +333,8 @@ run (const gchar *name,
gint i = 5;
if (nparams > i)
{
memset (xsvals.comment, 0, sizeof (xsvals.comment));
strncpy (xsvals.comment, param[i].data.d_string,
g_strlcpy (xsvals.comment, param[i].data.d_string,
MAX_COMMENT);
}
i ++;
if (nparams > i)
@ -356,19 +353,15 @@ run (const gchar *name,
i ++;
if (nparams > i)
{
memset (xsvals.prefix, 0, sizeof (xsvals.prefix));
strncpy (xsvals.prefix, param[i].data.d_string,
g_strlcpy (xsvals.prefix, param[i].data.d_string,
MAX_PREFIX);
}
i += 2;
if (nparams > i)
{
xsvals.write_mask = param[i - 1].data.d_int32;
memset (xsvals.mask_ext, 0, sizeof (xsvals.mask_ext));
strncpy (xsvals.mask_ext, param[i].data.d_string,
MAX_MASK_EXT);
g_strlcpy (xsvals.mask_ext, param[i].data.d_string,
MAX_MASK_EXT);
}
i ++;
@ -391,9 +384,9 @@ run (const gchar *name,
{
gint size = gimp_parasite_data_size (parasite);
strncpy (xsvals.comment,
gimp_parasite_data (parasite), MIN (size, MAX_COMMENT));
xsvals.comment[MIN (size, MAX_COMMENT) + 1] = 0;
g_strlcpy (xsvals.comment,
gimp_parasite_data (parasite),
MIN (size, MAX_COMMENT));
gimp_parasite_free (parasite);
}
@ -1418,9 +1411,8 @@ static void
comment_entry_callback (GtkWidget *widget,
gpointer data)
{
memset (xsvals.comment, 0, sizeof (xsvals.comment));
strncpy (xsvals.comment,
gtk_entry_get_text (GTK_ENTRY (widget)), MAX_COMMENT);
g_strlcpy (xsvals.comment,
gtk_entry_get_text (GTK_ENTRY (widget)), MAX_COMMENT);
}
#endif
@ -1428,16 +1420,14 @@ static void
prefix_entry_callback (GtkWidget *widget,
gpointer data)
{
memset (xsvals.prefix, 0, sizeof (xsvals.prefix));
strncpy (xsvals.prefix,
gtk_entry_get_text (GTK_ENTRY (widget)), MAX_PREFIX);
g_strlcpy (xsvals.prefix,
gtk_entry_get_text (GTK_ENTRY (widget)), MAX_PREFIX);
}
static void
mask_ext_entry_callback (GtkWidget *widget,
gpointer data)
{
memset (xsvals.mask_ext, 0, sizeof (xsvals.mask_ext));
strncpy (xsvals.mask_ext,
gtk_entry_get_text (GTK_ENTRY (widget)), MAX_MASK_EXT);
g_strlcpy (xsvals.mask_ext,
gtk_entry_get_text (GTK_ENTRY (widget)), MAX_MASK_EXT);
}

View file

@ -491,8 +491,7 @@ browser_search (GimpBrowser *gimp_browser,
if ((utf8 = g_locale_to_utf8 (xtimestr, -1, NULL, NULL, NULL)))
{
strncpy (xtimestr, utf8, sizeof (xtimestr));
xtimestr[sizeof (xtimestr) - 1] = 0;
g_strlcpy (xtimestr, utf8, sizeof (xtimestr));
g_free (utf8);
}
}

View file

@ -734,7 +734,7 @@ dialog_load (GtkWidget *widget,
{
gchar *name = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
strncpy (qbist_info.path, name, PATH_MAX - 1);
g_strlcpy (qbist_info.path, name, PATH_MAX);
load_data (qbist_info.path);
g_free (name);
@ -779,7 +779,7 @@ dialog_save (GtkWidget *widget,
{
gchar *name = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
strncpy (qbist_info.path, name, PATH_MAX - 1);
g_strlcpy (qbist_info.path, name, PATH_MAX);
save_data (qbist_info.path);
g_free (name);

View file

@ -185,8 +185,7 @@ run (const gchar *name,
if (strlen (webpagevals.url) < MAX_URL_LEN)
{
strncpy (save.url, webpagevals.url, MAX_URL_LEN);
save.url[MAX_URL_LEN - 1] = 0;
g_strlcpy (save.url, webpagevals.url, MAX_URL_LEN);
}
else
{

View file

@ -508,10 +508,7 @@ static void
fits_set_error (const gchar *errmsg)
{
if (fits_n_error < FITS_MAX_ERROR)
{
strncpy (fits_error[fits_n_error], errmsg, FITS_ERROR_LENGTH);
fits_error[fits_n_error++][FITS_ERROR_LENGTH-1] = '\0';
}
g_strlcpy (fits_error[fits_n_error], errmsg, FITS_ERROR_LENGTH);
}

View file

@ -1576,8 +1576,7 @@ static void
gflare_name_copy (gchar *dest,
const gchar *src)
{
strncpy (dest, src, GFLARE_NAME_MAX - 1);
dest[GFLARE_NAME_MAX - 1] = '\0';
g_strlcpy (dest, src, GFLARE_NAME_MAX);
}
/*************************************************************************/
@ -4708,8 +4707,7 @@ static void
gradient_name_copy (gchar *dest,
const gchar *src)
{
strncpy (dest, src, GRADIENT_NAME_MAX - 1);
dest[GRADIENT_NAME_MAX - 1] = '\0';
g_strlcpy (dest, src, GRADIENT_NAME_MAX);
}
/*
@ -5063,8 +5061,7 @@ gradient_cache_lookup (const gchar *name,
while (gradient_cache_count >= GRADIENT_CACHE_SIZE)
gradient_cache_zorch();
ci = g_new (GradientCacheItem, 1);
strncpy (ci->name, name, GRADIENT_NAME_MAX - 1);
ci->name[GRADIENT_NAME_MAX - 1] = '\0';
g_strlcpy (ci->name, name, GRADIENT_NAME_MAX);
ci->next = gradient_cache_head;
ci->prev = NULL;
if (gradient_cache_head)

View file

@ -386,7 +386,7 @@ formatWindowsError (char *buffer,
(LPTSTR) &lpMsgBuf, 0, NULL );
/* Copy to the buffer */
strncpy(buffer, lpMsgBuf, buf_size - 1);
g_strlcpy (buffer, lpMsgBuf, buf_size);
LocalFree(lpMsgBuf);
}

View file

@ -370,7 +370,7 @@ script_fu_server_listen (gint timeout)
/* Associate the client address with the socket */
/* If all else fails ... */
strncpy (clientname, "(error during host address lookup)", NI_MAXHOST-1);
g_strlcpy (clientname, "(error during host address lookup)", NI_MAXHOST);
/* Lookup address */
(void) getnameinfo (&(client.sa), size, clientname, sizeof (clientname),