summaryrefslogtreecommitdiff
path: root/modules/printbackends
diff options
context:
space:
mode:
authorEmmanuele Bassi <ebassi@gnome.org>2018-04-10 14:51:27 +0100
committerEmmanuele Bassi <ebassi@gnome.org>2019-02-05 15:34:08 +0100
commit8f8b68f847eb59d99b82d848f428fea231f9ef96 (patch)
tree23f22e379000603533c4d1428ae216df4564a341 /modules/printbackends
parenteb732b3f68b4d0a96e9856696df9312161e450b8 (diff)
downloadgtk+-8f8b68f847eb59d99b82d848f428fea231f9ef96.tar.gz
Terminate strncpy() buffers correctly
When using strncpy() with a buffer we need to account for the terminating NUL character. GCC 8 started warning when using PPD_MAX_NAME as the buffer length for strncpy() because the buffer we're copying into has the same length — which means that the terminating NUL may be skipped if the source string has a length of PPD_MAX_NAME. The appropriate way to handle the case where we're copying a source with a length bigger than of PPD_MAX_NAME is, as reported in the strncpy() documentation, to copy `PPD_MAX_NAME - 1` bytes, and explicitly NUL terminate the destination buffer. This has the additional benefit of avoiding the compiler warning.
Diffstat (limited to 'modules/printbackends')
-rw-r--r--modules/printbackends/cups/gtkprintbackendcups.c8
1 files changed, 6 insertions, 2 deletions
diff --git a/modules/printbackends/cups/gtkprintbackendcups.c b/modules/printbackends/cups/gtkprintbackendcups.c
index 97c74d6bf4..f12b5eb60d 100644
--- a/modules/printbackends/cups/gtkprintbackendcups.c
+++ b/modules/printbackends/cups/gtkprintbackendcups.c
@@ -5645,7 +5645,10 @@ cups_printer_get_options (GtkPrinter *printer,
ppd_name = gtk_paper_size_get_ppd_name (paper_size);
if (ppd_name)
- strncpy (ppd_option->defchoice, ppd_name, PPD_MAX_NAME);
+ {
+ strncpy (ppd_option->defchoice, ppd_name, PPD_MAX_NAME - 1);
+ ppd_option->defchoice[PPD_MAX_NAME - 1] = '\0';
+ }
else
{
gchar *custom_name;
@@ -5664,7 +5667,8 @@ cups_printer_get_options (GtkPrinter *printer,
* 230.4x142.9"
*/
custom_name = g_strdup_printf (_("Custom %s×%s"), width, height);
- strncpy (ppd_option->defchoice, custom_name, PPD_MAX_NAME);
+ strncpy (ppd_option->defchoice, custom_name, PPD_MAX_NAME - 1);
+ ppd_option->defchoice[PPD_MAX_NAME - 1] = '\0';
g_free (custom_name);
}
}