Subject: 28.0.50; Warnings about snprintf in image.c on armv7l

From: "Basil L. Contovounesios" <[email-masked]>
Date: Thu, 23 Sep 2021 18:04:12 +0100

Severity: minor

Compiling image.c on a 32-bit armv7l host gives the following warnings:

--8<---------------cut here---------------start------------->8---
image.c: In function ‘svg_load_image’:
image.c:9999:64: warning: ‘%4d’ directive output may be truncated writing between 4 and 11 bytes
                 into a region of size between 7 and 8 [-Wformat-truncation=]
 9999 |       const char *css_spec = "svg{font-family:\"%s\";font-size:%4dpx}";
      |                                                                ^~~
image.c:10002:7: note: ‘snprintf’ output 37 or more bytes (assuming 38) into a destination of size 37
10002 |       snprintf (css, css_len, css_spec, img->face_font_family, img->face_font_size);
      |       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
image.c:10134:7: warning: ‘%f’ directive output may be truncated writing between 3 and 317 bytes
                 into a region of size between 167 and 187 [-Wformat-truncation=]
10134 |       "<svg xmlns:xlink=\"http://www.w3.org/1999/xlink\" "
      |       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
image.c:10138:22: note: format string is defined here
10138 |       "viewBox=\"0 0 %f %f\">"
      |                      ^~
image.c:10134:7: note: assuming directive output of 8 bytes
10134 |       "<svg xmlns:xlink=\"http://www.w3.org/1999/xlink\" "
      |       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
image.c:10134:7: note: assuming directive output of 8 bytes
image.c:10134:7: note: directive argument in the range [0, 16777215]
image.c:10134:7: note: assuming directive output of 1 byte
image.c:10161:27: note: ‘snprintf’ output 320 or more bytes (assuming 331) into a destination of size 383
10161 |         || buffer_size <= snprintf (wrapped_contents, buffer_size, wrapper,
      |                           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10162 |                                     foreground & 0xFFFFFF, width, height,
      |                                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10163 |                                     viewbox_width, viewbox_height,
      |                                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10164 |                                     background & 0xFFFFFF,
      |                                     ~~~~~~~~~~~~~~~~~~~~~~
10165 |                                     SSDATA (encoded_contents)))
      |                                     ~~~~~~~~~~~~~~~~~~~~~~~~~~
--8<---------------cut here---------------end--------------->8---

What's TRT to do?  Call snprintf twice, the first time to calculate the
required buffer size?  Calculate more precisely the resulting size of
each argument?  Inhibit the warnings?  Something else?

Some questions about the current code:

> const char *css_spec = "svg{font-family:\"%s\";font-size:%4dpx}";

Why specifically '%4d' for face_font_size?

> int css_len = strlen (css_spec) + strlen (img->face_font_family);
> css = xmalloc (css_len);
> snprintf (css, css_len, css_spec, img->face_font_family, img->face_font_size);
> rsvg_handle_set_stylesheet (rsvg_handle, (guint8 *)css, strlen (css), NULL);

Does css_len not need to include the terminating null byte?

What if xmalloc or snprintf fail?

> css = xmalloc (SBYTES (lcss) + 1);
> strncpy (css, SSDATA (lcss), SBYTES (lcss));
> *(css + SBYTES (lcss) + 1) = 0;

Can this be replaced with xlispstrdup?

Thanks,

-- 
Basil

In GNU Emacs 28.0.50 (build 1, armv7l-unknown-linux-gnueabihf, X toolkit, cairo version 1.16.0, Xaw3d scroll bars)
 of 2021-09-23 built on raspberrypi
Repository revision: 90547d370f7c75b95b1d1e64b84e624e752ea6e3
Repository branch: master
System Description: Raspbian GNU/Linux 11 (bullseye)

Configured using:
 'configure 'CC=ccache gcc' 'CFLAGS=-O2 -march=native' --config-cache
 --prefix=/home/pi/.local --enable-checking=structs
 --with-x-toolkit=lucid --with-file-notification=yes --with-x'

Configured features:
ACL CAIRO DBUS FREETYPE GIF GLIB GMP GNUTLS GPM GSETTINGS HARFBUZZ JPEG
JSON LCMS2 LIBOTF LIBSELINUX LIBSYSTEMD LIBXML2 M17N_FLT MODULES NOTIFY
INOTIFY PDUMPER PNG RSVG SECCOMP SOUND THREADS TIFF TOOLKIT_SCROLL_BARS
X11 XAW3D XDBE XIM XPM LUCID ZLIB

Important settings:
  value of $LC_ALL: en_IE.UTF-8
  value of $LANG: en_IE.UTF-8
  locale-coding-system: utf-8-unix

Major mode: Lisp Interaction

Minor modes in effect:
  tooltip-mode: t
  global-eldoc-mode: t
  eldoc-mode: t
  electric-indent-mode: t
  mouse-wheel-mode: t
  tool-bar-mode: t
  menu-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  blink-cursor-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t
  line-number-mode: t
  indent-tabs-mode: t
  transient-mark-mode: t

Load-path shadows:
None found.

Features:
(shadow sort mail-extr emacsbug message rmc puny dired dired-loaddefs
rfc822 mml mml-sec epa derived epg rfc6068 epg-config gnus-util rmail
rmail-loaddefs auth-source cl-seq eieio eieio-core cl-macs
eieio-loaddefs password-cache json map text-property-search time-date
subr-x seq mm-decode mm-bodies mm-encode mail-parse rfc2231 mailabbrev
gmm-utils mailheader cl-loaddefs cl-lib sendmail rfc2047 rfc2045
ietf-drums mm-util mail-prsvr mail-utils term/tmux term/xterm xterm
byte-opt gv bytecomp byte-compile cconv iso-transl tooltip eldoc
electric uniquify ediff-hook vc-hooks lisp-float-type elisp-mode mwheel
term/x-win x-win term/common-win x-dnd tool-bar dnd fontset image
regexp-opt fringe tabulated-list replace newcomment text-mode lisp-mode
prog-mode register page tab-bar menu-bar rfn-eshadow isearch easymenu
timer select scroll-bar mouse jit-lock font-lock syntax font-core
term/tty-colors frame minibuffer cl-generic cham georgian utf-8-lang
misc-lang vietnamese tibetan thai tai-viet lao korean japanese eucjp-ms
cp51932 hebrew greek romanian slovak czech european ethiopic indian
cyrillic chinese composite emoji-zwj charscript charprop case-table
epa-hook jka-cmpr-hook help simple abbrev obarray cl-preloaded nadvice
button loaddefs faces cus-face macroexp files window text-properties
overlay sha1 md5 base64 format env code-pages mule custom widget
hashtable-print-readable backquote threads dbusbind inotify lcms2
dynamic-setting system-font-setting font-render-setting cairo x-toolkit
x multi-tty make-network-process emacs)

Memory information:
((conses 8 54179 7628)
 (symbols 24 6739 1)
 (strings 16 18259 2216)
 (string-bytes 1 596762)
 (vectors 8 10587)
 (vector-slots 4 130733 8726)
 (floats 8 24 229)
 (intervals 28 202 1)
 (buffers 576 10))