[gtk] Terminate strncpy() buffers correctly
- From: Emmanuele Bassi <ebassi src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtk] Terminate strncpy() buffers correctly
- Date: Tue, 10 Apr 2018 13:58:35 +0000 (UTC)
commit 14c8a603e3b307d2f247764a828d390980854b0d
Author: Emmanuele Bassi <ebassi gnome org>
Date: Tue Apr 10 14:51:27 2018 +0100
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.
modules/printbackends/gtkprintbackendcups.c | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)
---
diff --git a/modules/printbackends/gtkprintbackendcups.c b/modules/printbackends/gtkprintbackendcups.c
index 09e18b170e..52614069d2 100644
--- a/modules/printbackends/gtkprintbackendcups.c
+++ b/modules/printbackends/gtkprintbackendcups.c
@@ -5590,7 +5590,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;
@@ -5609,7 +5612,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);
}
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]