SECURE_CODING: Use snprintf/write_fmt_str instead of sprintf
authorNelson Ferreira <nelson.ferreira@ieee.org>
Mon, 10 Oct 2011 02:56:14 +0000 (22:56 -0400)
committerNelson Ferreira <nelson.ferreira@ieee.org>
Mon, 10 Oct 2011 02:56:14 +0000 (22:56 -0400)
* etc/tests/external-widget/test-ew-motif.c (ScaleValueChangedCB):
Use snprintf instead of sprintf.
* etc/tests/external-widget/test-ew-motif.c (main): ditto.
* etc/tests/external-widget/test-ew-motif.c (main): ditto.
* etc/tests/external-widget/test-ew-xlib.c (draw_text): ditto
* lib-src/gnuclient.c (tell_emacs_to_resume): ditto.
* lib-src/gnuclient.c (main): ditto.
* lib-src/gnuserv.c (ipc_init): ditto.
* lib-src/gnuserv.c (unix_init): ditto.
* lib-src/gnuslib.c (connect_to_ipc_server): ditto.
* lib-src/gnuslib.c (connect_to_unix_server): ditto.
* lib-src/make-po.c (buf_print): ditto.
* lib-src/movemail.c (lock_dot): ditto.
* lib-src/pop.c (pop_open): ditto.
* lib-src/profile.c (get_time): ditto.
* lib-src/yow.c (main): ditto.
* src/bytecode.c (invalid_byte_code_error): ditto.
* src/extents.c (print_extent_1): ditto.
* src/m/cydra5.h (PTY_NAME_SPRINTF): ditto.
* src/m/cydra5.h (PTY_TTY_NAME_SPRINTF): ditto.
* src/media/media-ffmpeg.c (media_ffmpeg_streaminfo): ditto.
* src/media/sound-hpplay.c (player_error_internal): ditto.
* src/media/sound-sunplay.c (init_device): ditto.
* src/media/sound-sunplay.c (play_sound_file): ditto.
* src/media/sound-sunplay.c (play_sound_data): ditto.
* src/media/sound-sunplay.c (sound_native_audio_init): ditto.
* src/process.c (make_process_internal): ditto.
* src/ui/Gtk/menubar-gtk.c (menu_descriptor_to_widget_1): ditto.

* src/print.c: fix comment

* modules/ase/ase-resclass.h (resc_rng_to_string): check return of
snprintf.
* modules/ase/ase-resclass.c (_ase_resc_rng_to_string): ditto.
* modules/ase/ase-resclass.c (_ase_resc_elm_to_string): ditto.
* src/media/sound-jack.c (sound_jack_subthread_create): ditto.

* src/effi.c (WRITE_FMT_STRING): new macro to use
write_fmt_string.

* src/lisp.h (PRIVATE_EXTERNAL_LIST_LOOP_6): Finally fix the
computed value not used warning.

* src/events/events.c (print_event): remove unneeded buf

* modules/ase/ase-cartesian.c (ase_cartesian_prnt): Use
write_fmt_str instead of sprintf.
* modules/ase/ase-heap.c (ase_yheap_prnt): ditto.
* modules/ase/ase-heap.c (ase_dheap_prnt): ditto.
* modules/ase/ase-heap.c (ase_wheap_prnt): ditto.
* modules/ase/ase-metric.c (ase_metric_prnt): ditto.
* modules/ase/ase-permutation.c (_ase_permutation_prnt_cyc): ditto.
* src/bytecode.c (print_compiled_function): ditto.
* src/chartab.c (print_chartab_two_byte_charset): ditto.
* src/chartab.c (print_char_table): ditto.
* src/database/database.c (print_database): ditto.
* src/database/eldap.c (print_ldap): ditto.
* src/database/postgresql.c (print_pgconn): ditto.
* src/database/postgresql.c (print_pgresult): ditto.
* src/effi.c (print_ffiobject): ditto.
* src/effi.c (print_ffi_job): ditto.
* src/emacs.c (fatal_error_signal): ditto.
* src/extents.c (print_extent_1): ditto.
* src/extents.c (print_extent): ditto.
* src/media/media.c (media_substream_print_audio): ditto.
* src/media/media.c (media_substream_print_video): ditto.
* src/media/sound-alsa.c (sound_alsa_print): ditto.
* src/media/sound-pulse.c (sound_pulse_print): ditto.

Signed-off-by: Nelson Ferreira <nelson.ferreira@ieee.org>
37 files changed:
etc/tests/external-widget/test-ew-motif.c
etc/tests/external-widget/test-ew-xlib.c
lib-src/gnuclient.c
lib-src/gnuserv.c
lib-src/gnuslib.c
lib-src/make-po.c
lib-src/movemail.c
lib-src/pop.c
lib-src/profile.c
lib-src/yow.c
modules/ase/ase-cartesian.c
modules/ase/ase-heap.c
modules/ase/ase-metric.c
modules/ase/ase-permutation.c
modules/ase/ase-resclass.c
modules/ase/ase-resclass.h
src/bytecode.c
src/chartab.c
src/database/database.c
src/database/eldap.c
src/database/postgresql.c
src/effi.c
src/emacs.c
src/events/events.c
src/extents.c
src/lisp.h
src/m/cydra5.h
src/media/media-ffmpeg.c
src/media/media.c
src/media/sound-alsa.c
src/media/sound-hpplay.c
src/media/sound-jack.c
src/media/sound-pulse.c
src/media/sound-sunplay.c
src/print.c
src/process.c
src/ui/Gtk/menubar-gtk.c

index 3fa9aa8..4333c73 100644 (file)
@@ -17,7 +17,8 @@ void ScaleValueChangedCB(Widget scale, XtPointer app_data,
        char labelarr[10];
        XmString labelstr;
 #if 0
-       sprintf(labelarr, "%d", xms->value);
+       int sz = snprintf(labelarr, sizeof(labelarr), "%d", xms->value);
+       assert(sz>=0 && sz<sizeof(labelarr));
        labelstr = XmStringCreateLocalized(labelarr);
        XtVaSetValues(label, XmNlabelString, labelstr, NULL);
        XmStringFree(labelstr);
@@ -68,7 +69,8 @@ main(int argc, char **argv)
        pushbutton = XmCreatePushButton(paned, "pushbutton", NULL, 0);
        text = XmCreateText(paned, "text", NULL, 0);
        for (i = 0; i < no_ews; i++) {
-               sprintf(buf, "extcli%d", i);
+               int sz = snprintf(buf, sizeof(buf), "extcli%d", i);
+               assert(sz>=0 && sz < sizeof(buf));
                emacscli[i] =
                    XtVaCreateWidget(buf, externalClientWidgetClass, paned,
                                     XmNwidth, 500, XmNheight, 200,
@@ -93,7 +95,9 @@ main(int argc, char **argv)
 
                strcpy(labarr, "window:");
                for (i = 0; i < no_ews; i++) {
-                       sprintf(tmpbuf, " %d", XtWindow(emacscli[i]));
+                       int sz = snprintf(tmpbuf, sizeof(tmpbuf), 
+                                         " %d", XtWindow(emacscli[i]));
+                       assert(sz>=0 && sz<sizeof(tmpbuf));
                        strcat(labarr, tmpbuf);
                }
                lab = XmStringCreateLocalized(labarr);
index d5db1e7..84e8f4d 100644 (file)
@@ -244,8 +244,8 @@ XFontStruct *font_info;
        XSetDashes(display, *gc, dash_offset, dash_list, list_length);
 }
 
-load_font(font_info)
-XFontStruct **font_info;
+void
+load_font(XFontStruct **font_info)
 {
        char *fontname = "9x15";
 
@@ -256,11 +256,8 @@ XFontStruct **font_info;
        }
 }
 
-draw_text(win, gc, font_info, win_width, win_height)
-Window win;
-GC gc;
-XFontStruct *font_info;
-unsigned int win_width, win_height;
+void
+draw_text(Window win, GC gc, XFontStruct *font_info, unsigned int win_width, unsigned int win_height)
 {
        char *string1 = "Hi! I'm a window, who are you?";
        char *string2 = "To terminate program; Press any key";
@@ -271,6 +268,7 @@ unsigned int win_width, win_height;
        char cd_height[50], cd_width[50], cd_depth[50];
        int font_height;
        int initial_y_offset, x_offset;
+       int sz;
 
        /* need length for both XTextWidth and XDrawString */
        len1 = strlen(string1);
@@ -293,12 +291,17 @@ unsigned int win_width, win_height;
                    (int)(win_height - font_height), string3, len3);
 
        /* copy numbers into string variables */
-       (void)sprintf(cd_height, " Height - %d pixels",
+       sz = snprintf(cd_height, sizeof(cd_height), " Height - %d pixels",
                      DisplayHeight(display, screen_num));
-       (void)sprintf(cd_width, " Width  - %d pixels",
+       assert(sz>=0 && sz<sizeof(cd_height));
+
+       sz = snprintf(cd_width, sizeof(cd_width), " Width  - %d pixels",
                      DisplayWidth(display, screen_num));
-       (void)sprintf(cd_depth, " Depth  - %d plane(s)",
+       assert(sz>=0 && sz<sizeof(cd_width));
+
+       sz = snprintf(cd_depth, sizeof(cd_depth), " Depth  - %d plane(s)",
                      DefaultDepth(display, screen_num));
+       assert(sz>=0 && sz<sizeof(cd_depth));
 
        /* reuse these for same purpose */
        len4 = strlen(string4);
index 671f3a7..f83a607 100644 (file)
@@ -58,6 +58,7 @@ char gnuserv_version[] = "gnuclient version " GNUSERV_VERSION;
 #include <stdlib.h>
 #include <sys/types.h>
 #include <sysfile.h>
+#include <assert.h>
 
 #ifdef HAVE_STRING_H
 #include <string.h>
@@ -91,6 +92,7 @@ void initialize_signals(void);
 static void tell_emacs_to_resume(int sig)
 {
        char buffer[GSERV_BUFSZ + 1];
+       int sz;
        int s;                  /* socket / msqid to server */
        int connect_type;       /* CONN_UNIX, CONN_INTERNET, or
                                   ONN_IPC */
@@ -103,8 +105,9 @@ static void tell_emacs_to_resume(int sig)
 
        connect_type = make_connection(NULL, 0, &s);
 
-       sprintf(buffer, "(gnuserv-eval '(resume-pid-console %d))",
-               (int)getpid());
+       sz = snprintf(buffer, sizeof(buffer), "(gnuserv-eval '(resume-pid-console %d))",
+                     (int)getpid());
+       assert(sz>=0 && sz<sizeof(buffer));
        send_string(s, buffer);
 
 #ifdef SYSV_IPC
@@ -309,6 +312,7 @@ int main(int argc, char *argv[])
        char buffer[GSERV_BUFSZ + 1];   /* buffer to read pid */
        char result[GSERV_BUFSZ + 1];
        int i;
+       int sz, msz;
 
 #ifdef INTERNET_DOMAIN_SOCKETS
        memset(remotepath, 0, sizeof(remotepath));
@@ -442,8 +446,9 @@ int main(int argc, char *argv[])
 #else
                connect_type = make_connection(NULL, 0, &s);
 #endif
-               sprintf(command, "(gnuserv-eval%s '(progn ",
-                       quick ? "-quickly" : "");
+               sz = snprintf(command, sizeof(command), "(gnuserv-eval%s '(progn ",
+                        quick ? "-quickly" : "");
+               assert(sz>=0 && sz<sizeof(command));
                send_string(s, command);
                if (load_library) {
                        send_string(s, "(load-library ");
@@ -477,8 +482,10 @@ int main(int argc, char *argv[])
 #else
                connect_type = make_connection(NULL, 0, &s);
 #endif
-               sprintf(command, "(gnuserv-eval%s '(progn ",
-                       quick ? "-quickly" : "");
+               sz = snprintf(command, sizeof(command),
+                             "(gnuserv-eval%s '(progn ",
+                             quick ? "-quickly" : "");
+               assert(sz>=0 && sz<sizeof(command));
                send_string(s, command);
 
                while ((nb = read(fileno(stdin), buffer, GSERV_BUFSZ - 1)) > 0) {
@@ -575,23 +582,27 @@ int main(int argc, char *argv[])
 
                if (suppress_windows_system) {
                        char *term = getenv("TERM");
+                       int sz;
                        if (!term) {
                                fprintf(stderr, "%s: unknown terminal type\n",
                                        progname);
                                exit(1);
                        }
-                       sprintf(command,
-                               "(gnuserv-edit-files '(tty %s %s %d) '(",
-                               clean_string(tty), clean_string(term),
-                               (int)getpid());
+                       sz = snprintf(command, sizeof(command),
+                                     "(gnuserv-edit-files '(tty %s %s %d) '(",
+                                     clean_string(tty), clean_string(term),
+                                     (int)getpid());
+                       assert(sz>=0 && sz<sizeof(command));
                } else {        /* !suppress_windows_system */
 
                        if (0) ;
 #ifdef HAVE_X_WINDOWS
-                       else if (display)
-                               sprintf(command,
-                                       "(gnuserv-edit-files '(x %s) '(",
-                                       clean_string(display));
+                       else if (display) {
+                               int sz = snprintf(command, sizeof(command),
+                                                 "(gnuserv-edit-files '(x %s) '(",
+                                                 clean_string(display));
+                               assert(sz>=0 && sz<sizeof(command));
+                       }
 #endif
 #ifdef HAVE_GTK
                        else if (display)
@@ -616,24 +627,28 @@ int main(int argc, char *argv[])
                        }
                        filename_expand(fullpath, argv[i], sizeof(fullpath));
 #ifdef INTERNET_DOMAIN_SOCKETS
-                       path =
-                           (char *)malloc(strlen(remotepath) +
-                                          strlen(fullpath) + 1);
-                       sprintf(path, "%s%s", remotepath, fullpath);
+                       msz = strlen(remotepath) + strlen(fullpath) + 1;
+                       path = (char *)malloc(msz);
+                       sz = snprintf(path, msz, "%s%s", remotepath, fullpath);
+                       assert(sz>=0 && sz<msz);
 #else
                        path = my_strdup(fullpath);
 #endif
-                       sprintf(command, "(%d . %s)", starting_line,
-                               clean_string(path));
+                       sz = snprintf(command, sizeof(command),
+                                     "(%d . %s)", starting_line,
+                                     clean_string(path));
+                       assert(sz>=0 && sz<sizeof(command));
                        send_string(s, command);
                        free(path);
                }               /* for */
-
-               sprintf(command, ")%s%s",
-                       (quick
-                        || (nofiles
-                            && !suppress_windows_system)) ? " 'quick" : "",
-                       view ? " 'view" : "");
+               
+               sz = snprintf(command, sizeof(command),
+                             ")%s%s",
+                             (quick
+                              || (nofiles
+                                  && !suppress_windows_system)) ? " 'quick" : "",
+                             view ? " 'view" : "");
+               assert(sz>=0 && sz<sizeof(command));
                send_string(s, command);
                send_string(s, ")");
 
index c396fe9..1fabe54 100644 (file)
@@ -30,6 +30,7 @@
  */
 
 #include "gnuserv.h"
+#include <assert.h>
 
 char gnuserv_version[] = "gnuserv version" GNUSERV_VERSION;
 
@@ -128,7 +129,8 @@ void ipc_init(struct msgbuf **msgpp)
        key_t key;              /* messge key */
        char buf[GSERV_BUFSZ];  /* pathname for key */
 
-       sprintf(buf, "%s/gsrv%d", tmpdir, (int)geteuid());
+       int sz = snprintf(buf, sizeof(buf),"%s/gsrv%d", tmpdir, (int)geteuid());
+       assert(sz>=0 && sz<sizeof(buf));
        creat(buf, 0600);
        key = ftok(buf, 1);
 
@@ -703,6 +705,7 @@ static int unix_init(void)
        int ls;                 /* socket descriptor */
        struct sockaddr_un server;      /* unix socket address */
        socklen_t bindlen;
+       int sz;
 
        if ((ls = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
                perror(progname);
@@ -713,7 +716,9 @@ static int unix_init(void)
        /* if */
        /* Set up address structure for the listen socket. */
 #ifdef HIDE_UNIX_SOCKET
-       sprintf(server.sun_path, "%s/gsrvdir%d", tmpdir, (int)geteuid());
+       sz = snprintf(server.sun_path, sizeof(server.sun_path),
+                     "%s/gsrvdir%d", tmpdir, (int)geteuid());
+       assert(sz>=0 && sz<sizeof(server.sun_path));
        if (mkdir(server.sun_path, 0700) < 0) {
                /* assume it already exists, and try to set perms */
                if (chmod(server.sun_path, 0700) < 0) {
@@ -726,7 +731,9 @@ static int unix_init(void)
        strcat(server.sun_path, "/gsrv");
        unlink(server.sun_path);        /* remove old file if it exists */
 #else                          /* HIDE_UNIX_SOCKET */
-       sprintf(server.sun_path, "%s/gsrv%d", tmpdir, (int)geteuid());
+       sz = snprintf(server.sun_path, sizeof(server.sun_path),
+                     "%s/gsrv%d", tmpdir, (int)geteuid());
+       assert(sz>=0 && sz<sizeof(server.sun_path));
        unlink(server.sun_path);        /* remove old file if it exists */
 #endif                         /* HIDE_UNIX_SOCKET */
 
index 6e6bcd5..b056d6a 100644 (file)
@@ -130,7 +130,9 @@ static int connect_to_ipc_server(void)
        key_t key;              /* message key */
        char buf[GSERV_BUFSZ + 1];      /* buffer for filename */
 
-       sprintf(buf, "%s/gsrv%d", tmpdir, (int)geteuid());
+       int sz = snprintf(buf, sizeof(buf), 
+                         "%s/gsrv%d", tmpdir, (int)geteuid());
+       assert(sz>=0 && sz<sizeof(buf));
        creat(buf, 0600);
        if ((key = ftok(buf, 1)) == -1) {
                perror(progname);
@@ -245,6 +247,7 @@ static int connect_to_unix_server(void)
 {
        int s;                  /* connected socket descriptor */
        struct sockaddr_un server;      /* for unix connections */
+       int sz;
 
        if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
                perror(progname);
@@ -254,10 +257,13 @@ static int connect_to_unix_server(void)
 
        server.sun_family = AF_UNIX;
 #ifdef HIDE_UNIX_SOCKET
-       sprintf(server.sun_path, "%s/gsrvdir%d/gsrv", tmpdir, (int)geteuid());
+       sz = snprintf(server.sun_path, sizeof(server.sun_path),
+                     "%s/gsrvdir%d/gsrv", tmpdir, (int)geteuid());
 #else                          /* HIDE_UNIX_SOCKET */
-       sprintf(server.sun_path, "%s/gsrv%d", tmpdir, (int)geteuid());
+       sz = snprintf(server.sun_path, sizeof(server.sun_path),
+                     "%s/gsrv%d", tmpdir, (int)geteuid());
 #endif                         /* HIDE_UNIX_SOCKET */
+       assert(sz>=0 && sz<sizeof(server.sun_path));
        if (connect(s, (struct sockaddr *)&server, strlen(server.sun_path) + 2)
            < 0) {
                perror(progname);
index 2fadbe3..eea6dcb 100644 (file)
@@ -274,13 +274,14 @@ int buf_putc(buffer_struct * buffer, int c)
 */
 int buf_print(buffer_struct * buffer, const char *s)
 {
-       register int len;
+       register int len, sz, msz = buffer->size - buffer->index;
 
        len = strlen(s);
-       if (buffer->index + len >= buffer->size)
+       if ( len >= msz)
                return -1;
 
-       sprintf(&(buffer->buf[buffer->index]), s);
+       sz = snprintf(&(buffer->buf[buffer->index]), msz, "%s", s);
+       assert(sz>=0 && sz<msz);
        buffer->index += len;
        return len;
 }
index 70248f7..d328c45 100644 (file)
@@ -80,6 +80,8 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #include "../src/regex.h"
 #endif
 
+#include <assert.h>
+
 extern char *optarg;
 extern int optind, opterr;
 
@@ -594,10 +596,12 @@ static void lock_dot(char *filename)
 #endif
 
                if (desc < 0) {
-                       char *message = (char *)xmalloc(strlen(tempname) + 50);
-                       sprintf(message,
-                               "%s--see source file lib-src/movemail.c",
-                               tempname);
+                       int msz = strlen(tempname) + 50;
+                       char *message = (char *)xmalloc(msz);
+                       int sz = snprintf(message, msz,
+                                         "%s--see source file lib-src/movemail.c",
+                                         tempname);
+                       assert(sz>=0 && sz < msz);
                        pfatal_with_name(message);
                }
                close(desc);
index b4e3fba..5e88567 100644 (file)
@@ -175,6 +175,7 @@ pop_open (host, username, password, flags)
      int flags;
 {
        int sock;
+       int sz;
        popserver server;
 
        /* Determine the user name */
@@ -287,7 +288,8 @@ pop_open (host, username, password, flags)
                        "recompile pop.c with larger ERROR_MAX");
                return (0);
        }
-       sprintf (pop_error, "USER %s", username);
+       sz = snprintf (pop_error, sizeof(pop_error), "USER %s", username);
+       assert(sz>=0 && sz<sizeof(pop_error));
 
        if (sendline (server, pop_error) || getok (server)) {
                return (0);
@@ -300,7 +302,9 @@ pop_open (host, username, password, flags)
                        "recompile pop.c with larger ERROR_MAX");
                return (0);
        }
-       sprintf (pop_error, "PASS %s", password);
+       sz = snprintf (pop_error, sizeof(pop_error),
+                      "PASS %s", password);
+       assert(sz>=0 && sz<sizeof(pop_error));
 
        if (sendline (server, pop_error) || getok (server)) {
                return (0);
@@ -417,7 +421,8 @@ pop_list (server, message, IDs, sizes)
        }
 
        if (message) {
-               sprintf (pop_error, "LIST %d", message);
+               sz = snprintf (pop_error, sizeof(pop_error), "LIST %d", message);
+               assert(sz>=0 && sz<sizeof(pop_error));
                if (sendline (server, pop_error)) {
                        free ((char *) *IDs);
                        free ((char *) *sizes);
@@ -594,7 +599,8 @@ pop_retrieve_first (server, message, response)
      int message;
      char **response;
 {
-       sprintf (pop_error, "RETR %d", message);
+       int sz = snprintf (pop_error, sizeof(pop_error), "RETR %d", message);
+       assert(sz>=0 && sz<sizeof(pop_error));
        return (pop_multi_first (server, pop_error, response));
 }
 
@@ -627,7 +633,9 @@ pop_top_first (server, message, lines, response)
      int message, lines;
      char **response;
 {
-       sprintf (pop_error, "TOP %d %d", message, lines);
+       int sz = snprintf (pop_error, sizeof(pop_error),
+                          "TOP %d %d", message, lines);
+       assert(sz>=0 && sz<sizeof(pop_error));
        return (pop_multi_first (server, pop_error, response));
 }
 
@@ -763,12 +771,16 @@ pop_delete (server, message)
      popserver server;
      int message;
 {
+       int sz;
+
        if (server->in_multi) {
                strcpy (pop_error, "In multi-line query in pop_delete");
                return (-1);
        }
 
-       sprintf (pop_error, "DELE %d", message);
+       sz = snprintf (pop_error, sizeof(pop_error),
+                      "DELE %d", message);
+       assert(sz>=0 && sz<sizeof(pop_error));
 
        if (sendline (server, pop_error) || getok (server))
                return (-1);
index 9429ab3..8f9c324 100644 (file)
 #include <config.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <assert.h>
 #include "../src/systime.h"
 
 static struct timeval TV1, TV2;
 static int watch_not_started = 1;      /* flag */
-static char time_string[30];
+static char time_string[64];
 
 /* Reset the stopwatch to zero.  */
 
@@ -53,6 +54,7 @@ static void reset_watch(void)
 
 static char *get_time(void)
 {
+       int sz;
        if (watch_not_started)
                exit(1);        /* call reset_watch first ! */
        EMACS_GET_TIME(TV2);
@@ -60,9 +62,10 @@ static char *get_time(void)
                TV2.tv_usec += 1000000;
                TV2.tv_sec--;
        }
-       sprintf(time_string, "%lu.%06lu",
-               (unsigned long)TV2.tv_sec - TV1.tv_sec,
-               (unsigned long)TV2.tv_usec - TV1.tv_usec);
+       sz = snprintf(time_string, sizeof(time_string), "%lu.%06lu",
+                     (unsigned long)TV2.tv_sec - TV1.tv_sec,
+                     (unsigned long)TV2.tv_usec - TV1.tv_usec);
+       assert(sz>=0 && sz<sizeof(time_string));
        return time_string;
 }
 
index ce4d1a0..8f7df31 100644 (file)
@@ -44,15 +44,16 @@ int main(int argc, char *argv[])
        } else
 #ifdef PATH_DATA
 #ifdef vms
-               sprintf(file, "%s%s", PATH_DATA, YOW_FILE);
+               int sz = snprintf(file, sizeof(file), "%s%s", PATH_DATA, YOW_FILE);
 #else
-               sprintf(file, "%s/%s", PATH_DATA, YOW_FILE);
+               int sz = snprintf(file, sizeof(file), "%s/%s", PATH_DATA, YOW_FILE);
 #endif
+               assert(sz>=0 && sz<sizeof(file));
 #else                          /* !PATH_DATA */
        {
                fprintf(stderr,
                        "%s: the location of the \"%s\" file was not supplied at compile-time.\n\
-        You must supply it with the -f command-line option.\n",
+You must supply it with the -f command-line option.\n",
                        argv[0], YOW_FILE);
                exit(1);
        }
index fb3205c..2432548 100644 (file)
@@ -95,12 +95,8 @@ ase_cartesian_prnt(Lisp_Object obj, Lisp_Object pcf, int unused)
        if (!NILP(XASE_CARTESIAN_INTERIOR_TYPE(obj))) {
                print_internal(XASE_CARTESIAN_INTERIOR_TYPE(obj), pcf, unused);
        }
-       {
-               char dim[64];
-               snprintf(dim, 63, " of dimension %d, ",
-                        XASE_CARTESIAN_DIMENSION(obj));
-               write_c_string(dim, pcf);
-       }
+       write_fmt_str(pcf, " of dimension %d, ",
+                     XASE_CARTESIAN_DIMENSION(obj));
        _ase_cartesian_prnt(XASE_CARTESIAN(obj), pcf);
 
        write_c_string(">", pcf);
index 72216e0..0066bc6 100644 (file)
@@ -190,7 +190,6 @@ _ase_yheap_prnt(ase_yheap_t a, Lisp_Object pcf)
 static void
 ase_yheap_prnt(Lisp_Object obj, Lisp_Object pcf, int SXE_UNUSED(foo))
 {
-       char siz[128];
        ase_yheap_t h = XASE_YHEAP(obj);
 
        EMOD_ASE_DEBUG_HEAP("h:0x%08lx@0x%08lx shall be printed...\n",
@@ -198,8 +197,7 @@ ase_yheap_prnt(Lisp_Object obj, Lisp_Object pcf, int SXE_UNUSED(foo))
        write_c_string("#<ase:heap :dynamic", pcf);
 
        write_c_string(" :size ", pcf);
-       snprintf(siz, 127, "%u", (unsigned int)ase_yheap_size(h));
-       write_c_string(siz, pcf);
+       write_fmt_str(pcf, "%u", (unsigned int)ase_yheap_size(h));
 
        if (ase_yheap_root(h) != NULL &&
            ase_yheap_cell_data(ase_yheap_root(h)) != Qnull_pointer) {
@@ -229,16 +227,13 @@ _ase_dheap_prnt(ase_dheap_t h, Lisp_Object pcf)
 static void
 ase_dheap_prnt(Lisp_Object obj, Lisp_Object pcf, int SXE_UNUSED(foo))
 {
-       char siz[128];
        ase_dheap_t h = XASE_DHEAP(obj);
 
        EMOD_ASE_DEBUG_HEAP("d:0x%08lx@0x%08lx shall be printed...\n",
                            (long unsigned int)h, (long unsigned int)obj);
-       write_c_string("#<ase:heap :dense", pcf);
 
-       write_c_string(" :size ", pcf);
-       snprintf(siz, 127, "%u", (unsigned int)ase_dheap_size(h));
-       write_c_string(siz, pcf);
+       write_fmt_str(pcf, "#<ase:heap :dense :size %u", 
+                     (unsigned int)ase_dheap_size(h));
 
        if (ase_heap_opts_coloured(h)) {
                write_c_string(" :coloured", pcf);
@@ -271,16 +266,12 @@ _ase_wheap_prnt(ase_wheap_t h, Lisp_Object pcf)
 static void
 ase_wheap_prnt(Lisp_Object obj, Lisp_Object pcf, int SXE_UNUSED(foo))
 {
-       char siz[128];
        ase_wheap_t h = XASE_WHEAP(obj);
 
        EMOD_ASE_DEBUG_HEAP("w:0x%08lx@0x%08lx shall be printed...\n",
                            (long unsigned int)h, (long unsigned int)obj);
-       write_c_string("#<ase:heap :weak", pcf);
-
-       write_c_string(" :size ", pcf);
-       snprintf(siz, 127, "%u", (unsigned int)ase_wheap_size(h));
-       write_c_string(siz, pcf);
+       write_fmt_string(pcf, "#<ase:heap :weak :size %u", 
+                        (unsigned int)ase_wheap_size(h));
 
        if (ase_heap_opts_coloured(h)) {
                write_c_string(" :coloured", pcf);
index b61c0ee..9ea995c 100644 (file)
@@ -70,23 +70,21 @@ ase_metric_prnt(Lisp_Object obj, Lisp_Object pcf, int unused)
        write_c_string("#<", pcf);
        print_internal(XDYNACAT_TYPE(obj), pcf, unused);
        {
-               char addr[64];
                if (NILP(XASE_METRIC_LDIST(obj))) {
-                       snprintf(addr, 63, " %p", XASE_METRIC_DIST(obj));
+                       write_hex_ptr(XASE_METRIC_DIST(obj),pcf);
                } else {
                        Lisp_Object ldist = XASE_METRIC_LDIST(obj);
                        if (SYMBOLP(ldist)) {
                                Lisp_String *name =
                                        symbol_name(XSYMBOL(ldist));
-                               snprintf(addr, 63, " #'%s", string_data(name));
+                               write_fmt_string(pcf, " #'%s", string_data(name));
                        } else if (SUBRP(ldist)) {
                                const char *name = subr_name(XSUBR(ldist));
-                               snprintf(addr, 63, " #'%s", name);
+                               write_fmt_string(pcf, " #'%s", name);
                        } else {
-                               snprintf(addr, 63, " #'(lambda ...)");
+                               write_c_string(" #'(lambda ...)", pcf);
                        }
                }
-               write_c_string(addr, pcf);
        }
        write_c_string(">", pcf);
        return;
index 90215e8..653c3ca 100644 (file)
@@ -57,17 +57,10 @@ static void
 _ase_permutation_prnt_cyc(unsigned long *p, unsigned long idx, Lisp_Object pcf)
 {
        unsigned long q;
-       char *tmp = alloca_array(char, sane_small);
 
-       write_c_string("(", pcf);
-       snprintf(tmp, sane_small, "%ld", idx+1);
-       write_c_string(tmp, pcf);
-
-       for (q = p[idx]; q != idx; q = p[q]) {
-               write_c_string(" ", pcf);
-               snprintf(tmp, sane_small, "%ld", q+1);
-               write_c_string(tmp, pcf);
-       }
+       write_fmt_string(pcf, "(%ld", idx+1);
+       for (q = p[idx]; q != idx; q = p[q])
+               write_fmt_str(pcf, " %ld", q+1);
        write_c_string(")", pcf);
 }
 
index d275bac..44a33e8 100644 (file)
@@ -73,9 +73,10 @@ _resc_rng_buffer_size(ase_resc_rng_t a)
 static inline void
 _ase_resc_rng_to_string(char *buf, int len, ase_resc_rng_t a)
 {
-       if (ase_resc_rng_smallp(a))
-               snprintf(buf, len, "%ld", a->small_ring);
-       else
+       if (ase_resc_rng_smallp(a)) {
+               int sz = snprintf(buf, len, "%ld", a->small_ring);
+               assert(sz >= 0 && sz < len);
+       } else
                resc_rng_to_string(buf, len, ase_resc_rng_ring(a));
        return;
 }
@@ -124,9 +125,10 @@ _resc_elm_buffer_size(ase_resc_elm_t a)
 static inline void
 _ase_resc_elm_to_string(char *buf, int len, ase_resc_elm_t a)
 {
-       if (ase_resc_elm_smallp(a))
-               snprintf(buf, len, "%ld", a->small_data);
-       else
+       if (ase_resc_elm_smallp(a)) {
+               int sz = snprintf(buf, len, "%ld", a->small_data);
+               assert(sz>=0 && sz < len);
+       } else
                resc_elm_to_string(buf, len, ase_resc_elm_data(a));
        return;
 }
index c7b1fb3..9e44d64 100644 (file)
@@ -162,8 +162,8 @@ extern Lisp_Object _ase_wrap_resc_elm(ase_resc_elm_t);
 #define resc_rng_to_string(_p, _n, _r) mpz_get_str(_p, 10, _r)
 #define resc_elm_to_string(_p, _n, _e) mpz_get_str(_p, 10, _e)
 #else
-#define resc_rng_to_string(_p, _n, _r) snprintf(_p, _n, "%ld", _r)
-#define resc_elm_to_string(_p, _n, _e) snprintf(_p, _n, "%ld", _e)
+#define resc_rng_to_string(_p, _n, _r) do { int sz = snprintf(_p, _n, "%ld", _r); assert(sz>=0 && sz<_n); } while(0)
+#define resc_elm_to_string(_p, _n, _e) do { int sz = snprintf(_p, _n, "%ld", _e); assert(sz>=0 && sz<_n); } while(0)
 #endif
 
 
index 8f6769b..f17d058 100644 (file)
@@ -1328,9 +1328,11 @@ static void invalid_byte_code_error(char *error_message, ...)
 {
        Lisp_Object obj;
        va_list args;
-       char *buf = alloca_array(char, strlen(error_message) + 128);
+       int maxsz = strlen(error_message) + 128;
+       char *buf = alloca_array(char, maxsz);
 
-       sprintf(buf, "%s", error_message);
+       int sz=snprintf(buf, maxsz, "%s", error_message);
+       assert(sz>=0 && sz<maxsz);
        va_start(args, error_message);
        obj = emacs_doprnt_string_va((const Bufbyte *)GETTEXT(buf), Qnil, -1,
                                     args);
@@ -1790,7 +1792,6 @@ print_compiled_function(Lisp_Object obj, Lisp_Object printcharfun,
        int docp = f->flags.documentationp;
        int intp = f->flags.interactivep;
        struct gcpro gcpro1, gcpro2;
-       char buf[100];
        GCPRO2(obj, printcharfun);
 
        write_c_string(print_readably ? "#[" : "#<compiled-function ",
@@ -1816,9 +1817,8 @@ print_compiled_function(Lisp_Object obj, Lisp_Object printcharfun,
                NGCPRO1(instructions);
                if (STRINGP(instructions) && !print_readably) {
                        /* We don't usually want to see that junk in the bytecode. */
-                       sprintf(buf, "\"...(%ld)\"",
-                               (long)XSTRING_CHAR_LENGTH(instructions));
-                       write_c_string(buf, printcharfun);
+                       write_fmt_str(printcharfun, "\"...(%ld)\"",
+                                     (long)XSTRING_CHAR_LENGTH(instructions));
                } else
                        print_internal(instructions, printcharfun, escapeflag);
                NUNGCPRO;
@@ -1830,8 +1830,7 @@ print_compiled_function(Lisp_Object obj, Lisp_Object printcharfun,
                       escapeflag);
 
        /* COMPILED_STACK_DEPTH = 3 */
-       sprintf(buf, " %d", compiled_function_stack_depth(f));
-       write_c_string(buf, printcharfun);
+       write_fmt_str(printcharfun, " %d", compiled_function_stack_depth(f));
 
        /* COMPILED_DOC_STRING = 4 */
        if (docp || intp) {
index 6bf3f43..49e4189 100644 (file)
@@ -293,12 +293,9 @@ print_chartab_two_byte_charset(Lisp_Object charset,
                Lisp_Object jen = cte->level2[i - 32];
 
                if (!CHAR_TABLE_ENTRYP(jen)) {
-                       char buf[100];
-
                        write_c_string(" [", printcharfun);
                        print_internal(XCHARSET_NAME(charset), printcharfun, 0);
-                       sprintf(buf, " %d] ", i);
-                       write_c_string(buf, printcharfun);
+                       write_fmt_str(printcharfun, " %d] ", i);
                        print_internal(jen, printcharfun, 0);
                } else
                        print_chartab_charset_row(charset, i,
@@ -313,12 +310,10 @@ static void
 print_char_table(Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
 {
        Lisp_Char_Table *ct = XCHAR_TABLE(obj);
-       char buf[200];
        Lisp_Object tmp_sym = char_table_type_to_symbol(ct->type);
 
-       snprintf(buf, countof(buf)-1, "#s(char-table type %s data (",
-                string_data(symbol_name(XSYMBOL(tmp_sym))));
-       write_c_string(buf, printcharfun);
+       write_fmt_string(printcharfun, "#s(char-table type %s data (",
+                        string_data(symbol_name(XSYMBOL(tmp_sym))));
 
        /* Now write out the ASCII/Control-1 stuff. */
        {
index 08b592b..d1174d3 100644 (file)
@@ -162,7 +162,6 @@ static Lisp_Object mark_database(Lisp_Object object)
 static void
 print_database(Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
 {
-       char buf[85];
        Lisp_Database *db = XDATABASE(obj);
        Lisp_Object tmp1, tmp2;
 
@@ -175,14 +174,14 @@ print_database(Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
 
        tmp1 = db->funcs->get_type(db);
        tmp2 = db->funcs->get_subtype(db);
-       snprintf(buf, sizeof(buf), "\" (%s/%s/%s) 0x%x>",
-                 (char *)string_data(XSYMBOL(tmp1)->name),
-                 (char *)string_data(XSYMBOL(tmp2)->name),
-                 (!DATABASE_LIVE_P(db) ? "closed" :
-                  (db->access_ & O_WRONLY) ? "writeonly" :
-                  (db->access_ & O_RDWR) ? "readwrite" : "readonly"),
-                 db->header.uid);
-       write_c_string(buf, printcharfun);
+       write_fmt_string(printcharfun, 
+                        "\" (%s/%s/%s) 0x%x>",
+                        (char *)string_data(XSYMBOL(tmp1)->name),
+                        (char *)string_data(XSYMBOL(tmp2)->name),
+                        (!DATABASE_LIVE_P(db) ? "closed" :
+                         (db->access_ & O_WRONLY) ? "writeonly" :
+                         (db->access_ & O_RDWR) ? "readwrite" : "readonly"),
+                        db->header.uid);
 }
 
 static void finalize_database(void *header, int for_disksave)
index 3d4c8cc..182da91 100644 (file)
@@ -112,8 +112,7 @@ print_ldap(Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
        print_internal(ldap->host, printcharfun, 1);
        if (!ldap->ld)
                write_c_string("(dead) ", printcharfun);
-       sprintf(buf, " 0x%lx>", (long)ldap);
-       write_c_string(buf, printcharfun);
+       write_fmt_string(printcharfun, " 0x%lx>", (long)ldap);
 }
 
 static Lisp_LDAP *allocate_ldap(void)
index 091f1f4..d125335 100644 (file)
@@ -232,7 +232,6 @@ mark_pgconn(Lisp_Object obj)
 static void
 print_pgconn(Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
 {
-       char buf[256];
        PGconn *P;
        ConnStatusType cst;
        char *host = "", *db = "", *user = "", *port = "";
@@ -241,7 +240,7 @@ print_pgconn(Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
 
        if (P == NULL) {
                /* this may happen since we allow PQfinish() to be called */
-               strncpy(buf, "#<PGconn DEAD>", countof(buf));
+               write_c_string("#<PGconn DEAD>", printcharfun);
        } else if ((cst = PQstatus(P)) == CONNECTION_OK) {
                if (!(host = PQhost(P)))
                        host = "";
@@ -250,14 +249,15 @@ print_pgconn(Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
                if (!(user = PQuser(P))) {
                        user = "";
                }
-               snprintf(buf, sizeof(buf), "#<PGconn %s:%s %s/%s>",     /* evil! */
-                       !strlen(host) ? "localhost" : host, port, user, db);
+               write_fmt_string(printcharfun, "#<PGconn %s:%s %s/%s>", 
+                                (!strlen(host) ? 
+                                 "localhost" : host) /* evil! */, 
+                                port, user, db);
        } else if (cst == CONNECTION_BAD) {
-               strncpy(buf, "#<PGconn BAD>", countof(buf));
+               write_c_string("#<PGconn BAD>", printcharfun);
        } else {
-               strncpy(buf, "#<PGconn connecting>", countof(buf));
+               write_c_string("#<PGconn connecting>", printcharfun);
        }
-       write_c_string(buf, printcharfun);
        return;
 }
 
@@ -318,7 +318,6 @@ mark_pgresult(Lisp_Object obj)
 static void
 print_pgresult(Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
 {
-       char buf[1024];
        PGresult *res;
 
        res = (XPGRESULT(obj))->pgresult;
@@ -327,32 +326,30 @@ print_pgresult(Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
                switch (PQresultStatus(res)) {
                case PGRES_TUPLES_OK:
                        /* Add number of tuples of result to output */
-                       snprintf(buf, countof(buf), RESULT_TUPLES_FMT,
-                                PQresStatus(PQresultStatus(res)),
-                                PQntuples(res), PQcmdStatus(res));
+                       write_fmt_string(printcharfun, RESULT_TUPLES_FMT,
+                                        PQresStatus(PQresultStatus(res)),
+                                        PQntuples(res), PQcmdStatus(res));
                        break;
                case PGRES_COMMAND_OK:
                        /* Add number of tuples affected by output-less
                           command */
                        if (!strlen(PQcmdTuples(res)))
                                goto notuples;
-                       snprintf(buf, countof(buf), RESULT_CMD_TUPLES_FMT,
-                                PQresStatus(PQresultStatus(res)),
-                                PQcmdTuples(res), PQcmdStatus(res));
+                       write_fmt_string(printcharfun, RESULT_CMD_TUPLES_FMT,
+                                        PQresStatus(PQresultStatus(res)),
+                                        PQcmdTuples(res), PQcmdStatus(res));
                        break;
                default:
                notuples:
                        /* No counts to print */
-                       snprintf(buf, countof(buf), RESULT_DEFAULT_FMT,
-                                PQresStatus(PQresultStatus(res)),
-                                PQcmdStatus(res));
+                       write_fmt_string(printcharfun, RESULT_DEFAULT_FMT,
+                                        PQresStatus(PQresultStatus(res)),
+                                        PQcmdStatus(res));
                        break;
                }
        } else {
-               strncpy(buf, "#<PGresult DEAD>", countof(buf));
+               write_c_string("#<PGresult DEAD>", printcharfun);
        }
-
-       write_c_string(buf, printcharfun);
        return;
 }
 
index 0597ac8..ded16d4 100644 (file)
@@ -55,11 +55,19 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>. */
 #  define SIGNAL_ERROR signal_error
 #  define FFIBYTE Bufbyte
 #  define WRITE_C_STRING(x,y) write_c_string((x),(y))
+#  define WRITE_FMT_STRING(x,y,...) write_fmt_string((x),(y),__VA_ARGS__)
 #  define LRECORD_DESCRIPTION lrecord_description
 #else
 #  define SIGNAL_ERROR Fsignal
 #  define FFIBYTE Ibyte
 #  define WRITE_C_STRING(x,y) write_c_string((y),(x))
+#  define WRITE_FMT_STRING(x,y,...)                    \
+       do {                                            \
+               char wcsb[128];                         \
+               int wcss = snprintf(wcsb, sizeof(wcsb), \
+                                   (y),__VA_ARGS__);   \
+               write_c_string((y),wcsb);               \
+       } while(0)
 #  define LRECORD_DESCRIPTION memory_description
 #endif /* SXEMACS */
 
@@ -143,8 +151,6 @@ print_ffiobject(Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
 {
        /* This function can GC */
        Lisp_EffiObject *ffio = XEFFIO(obj);
-       char buf[256];
-
         escapeflag = escapeflag;        /* shutup compiler */
        if (print_readably) {
 #ifdef SXEMACS
@@ -163,9 +169,8 @@ print_ffiobject(Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
                 print_internal(ffio->type, printcharfun, 1);
                 WRITE_C_STRING(" ", printcharfun);
         }
-       snprintf(buf, 255, "size=%ld fotype=%d foptr=%p>",
-                 (long)XINT(ffio->size), ffio->fotype, ffio->fop.generic);
-       WRITE_C_STRING(buf, printcharfun);
+       WRITE_FMT_STRING(printcharfun,"size=%ld fotype=%d foptr=%p>",
+                        (long)XINT(ffio->size), ffio->fotype, ffio->fop.generic);
 }
 
 static const struct LRECORD_DESCRIPTION ffiobject_description[] = {
@@ -1497,12 +1502,10 @@ static void
 print_ffi_job(worker_job_t job, Lisp_Object pcf)
 {
        ffi_job_t ffij = ffi_job(job);
-       char *str = alloca(64);
 
        SXE_MUTEX_LOCK(&ffij->mtx);
-       WRITE_C_STRING(" carrying ", pcf);
-       snprintf(str, 63, " #<ffi-job 0x%lx>", (long unsigned int)ffij);
-       WRITE_C_STRING(str, pcf);
+       WRITE_FMT_STRING(pcf, " carrying  #<ffi-job 0x%lx>", 
+                        (long unsigned int)ffij);
        SXE_MUTEX_UNLOCK(&ffij->mtx);
        return;
 }
index d426640..555fa35 100644 (file)
@@ -633,11 +633,17 @@ fatal_error_signal(int sig)
                        const char *pstack = "/usr/proc/bin/pstack";
                        if (access(pstack, X_OK) == 0) {
                                char buf[100];
+                               int sz = snprintf(buf, sizeof(buf), "%s %d >&2", pstack,
+                                       (int)getpid());
                                stderr_out("\nC backtrace follows:\n"
                                           "(A real debugger may provide better information)\n\n");
-                               sprintf(buf, "%s %d >&2", pstack,
-                                       (int)getpid());
-                               system(buf);
+                               if ( sz >= 0 && sz < sizeof(buf)) {
+                                       sz = system(buf);
+                                       if ( sz != 0 )
+                                               stderr_out("\nStacktrace utility execution error code: %d\n", sz);
+                               } else {
+                                       stderr_out("\nCould not build command line for stacktrace utility.\n");
+                               }
                        }
                }
 # endif
index 04aa180..ee07e4f 100644 (file)
@@ -256,7 +256,6 @@ print_event(Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
                print_event_1("#<magic-event ", obj, printcharfun);
                break;
        case pointer_motion_event: {
-               char buf[64];
                Lisp_Object Vx, Vy;
                Vx = Fevent_x_pixel(obj);
                assert(INTP(Vx));
index dda4c0d..e5c3ab8 100644 (file)
@@ -3127,6 +3127,7 @@ print_extent_1(Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
        EXTENT anc = extent_ancestor(ext);
        Lisp_Object tail;
        char buf[100], *bp = buf;
+       int sz;
 
        /* Retrieve the ancestor and use it, for faster retrieval of properties */
 
@@ -3134,11 +3135,13 @@ print_extent_1(Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
                *bp++ = '*';
        *bp++ = (extent_start_open_p(anc) ? '(' : '[');
        if (extent_detached_p(ext))
-               strcpy(bp, "detached");
-       else
-               sprintf(bp, "%ld, %ld",
-                       XINT(Fextent_start_position(obj)),
-                       XINT(Fextent_end_position(obj)));
+               strncpy(bp, "detached", sizeof(buf)-1);
+       else {
+               sz=snprintf(bp, sizeof(buf)-2, "%ld, %ld",
+                           XINT(Fextent_start_position(obj)),
+                           XINT(Fextent_end_position(obj)));
+               assert(sz>=0 && sz<(sizeof(buf)-2));
+       }
        bp += strlen(bp);
        *bp++ = (extent_end_open_p(anc) ? ')' : ']');
        if (!NILP(extent_end_glyph(anc)))
@@ -3173,8 +3176,7 @@ print_extent_1(Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
                write_c_string(" ", printcharfun);
        }
 
-       sprintf(buf, "0x%lx", (long)ext);
-       write_c_string(buf, printcharfun);
+       write_fmt_str(printcharfun, "0x%lx", (long)ext);
 }
 
 static void
@@ -3222,17 +3224,11 @@ print_extent(Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
                if (!EXTENT_LIVE_P(XEXTENT(obj))) {
                        write_c_string("#<destroyed extent", printcharfun);
                } else {
-                       /* C99ified */
-                       size_t bufsz = strlen(title) + strlen(name) +
-                               strlen(posttitle) + 1;
-                       char buf[bufsz];
-
                        write_c_string("#<extent ", printcharfun);
                        print_extent_1(obj, printcharfun, escapeflag);
                        write_c_string(extent_detached_p(XEXTENT(obj))
                                       ? " from " : " in ", printcharfun);
-                       snprintf(buf, bufsz, "%s%s%s", title, name, posttitle);
-                       write_c_string(buf, printcharfun);
+                       write_fmt_string(printcharfun, "%s%s%s", title, name, posttitle);
                }
        } else {
                if (print_readably)
index c7cadba..a2280c1 100644 (file)
@@ -639,18 +639,18 @@ PRIVATE_EXTERNAL_LIST_LOOP_6 (elt, list, len, tail,                       \
 
 #define PRIVATE_EXTERNAL_LIST_LOOP_6(elt, list, len, hare,             \
                                     tortoise, suspicion_length)        \
-  for (tortoise = hare = list, len = 0;                                        \
-                                                                       \
-       (CONSP (hare) ? ((void)(elt = XCAR (hare)), 1) :                        \
-       (NILP (hare) ? 0 :                                              \
-        ((void)signal_malformed_list_error (list), 0)));               \
+       for (tortoise = hare = list, len = 0;                           \
+                                                                       \
+            (CONSP (hare) ? ((elt = XCAR (hare)), 1) :                 \
+             (NILP (hare) ? 0 :                                        \
+              (signal_malformed_list_error (list), 0)));               \
                                                                        \
-       hare = XCDR (hare),                                             \
-        (void)                                                         \
-        ((++len > suspicion_length)                                    \
-         &&                                                            \
-         ((((len & 1) != 0) && (tortoise = XCDR (tortoise), 0)),       \
-          (EQ (hare, tortoise) && (signal_circular_list_error (list), 0)))))
+            (hare = XCDR (hare)),                                      \
+                    (void)((++len > suspicion_length) &&               \
+                           ((void)(((len & 1) != 0)&&                  \
+                                   ((tortoise = XCDR (tortoise)), 0)), \
+                            (EQ (hare, tortoise) &&                    \
+                             (signal_circular_list_error (list), 0)))))
 
 /* GET_LIST_LENGTH and GET_EXTERNAL_LIST_LENGTH:
 
index 2bdd3f3..d4e4ae7 100644 (file)
@@ -74,8 +74,8 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>. */
 /* Explain how pty filenames work.  */
 
 #define PTY_ITERATION  for (i = 47; i >= 0; i--)
-#define PTY_NAME_SPRINTF  sprintf (ptyname, "/dev/pty%03x", i);
-#define PTY_TTY_NAME_SPRINTF  sprintf (ptyname, "/dev/ptm%03x", i);
+#define PTY_NAME_SPRINTF  snprintf (ptyname, sizeof(ptyname), "/dev/pty%03x", i);
+#define PTY_TTY_NAME_SPRINTF  snprintf (ptyname, sizeof(ptyname), "/dev/ptm%03x", i);
 
 /* We can't do interrupt-driven input, so don't let user try.  */
 
index f3ea874..e1667a3 100644 (file)
@@ -157,9 +157,10 @@ char *media_ffmpeg_streaminfo(Lisp_Media_Stream *ms)
        }
        if (avfc->year) {
                char year[12];
+               int sz = snprintf(year, sizeof(year), "%d", avfc->year);
+               assert(sz>=0 && sz<sizeof(year));
                strncat(out, " :year ", chars_left);
                chars_left -= 7;
-               snprintf(year, 12, "%d", avfc->year);
                strncat(out, year, chars_left);
        }
 
index 78dcda8..4ce4417 100644 (file)
@@ -409,16 +409,13 @@ media_substream_print_audio(media_substream *mss, Lisp_Object printcharfun)
 
        write_c_string("#<audio ", printcharfun);
        if (mtap->name || mtap->codec_name) {
-               char *buf = alloca(64);
                if (mtap->name && mtap->codec_name)
-                       snprintf(buf, 63, "%s (%s)",
-                                mtap->name, mtap->codec_name);
+                       write_fmt_str(printcharfun, "%s (%s)",
+                                     mtap->name, mtap->codec_name);
                else if (mtap->name)
-                       snprintf(buf, 63, "%s [???]", mtap->name);
+                       write_fmt_str(printcharfun, "%s [???]", mtap->name);
                else if (mtap->codec_name)
-                       snprintf(buf, 63, "??? (%s)", mtap->codec_name);
-
-               write_c_string(buf, printcharfun);
+                       write_fmt_str(printcharfun, "??? (%s)", mtap->codec_name);
        } else
                write_c_string("???", printcharfun);
 
@@ -440,21 +437,13 @@ media_substream_print_audio(media_substream *mss, Lisp_Object printcharfun)
                break;
        }
 
-       if (mtap->samplerate) {
-               char *buf;
-               buf = alloca(48);
-               snprintf(buf, 47, ", %d Hz, %d Bit",
-                        mtap->samplerate,
-                        mtap->samplewidth);
-               write_c_string(buf, printcharfun);
-       }
+       if (mtap->samplerate) 
+               write_fmt_str(printcharfun, ", %d Hz, %d Bit",
+                             mtap->samplerate,
+                             mtap->samplewidth);
 
-       if (mtap->bitrate) {
-               char *buf;
-               buf = alloca(24);
-               snprintf(buf, 23, ", %d kb/s", mtap->bitrate/1000);
-               write_c_string(buf, printcharfun);
-       }
+       if (mtap->bitrate)
+               write_fmt_str(printcharfun, ", %d kb/s", mtap->bitrate/1000);
 
        write_c_string(">", printcharfun);
 }
@@ -467,36 +456,28 @@ media_substream_print_video(media_substream *mss, Lisp_Object printcharfun)
 
        write_c_string("#<video ", printcharfun);
        if (mtvp->name || mtvp->codec_name) {
-               char *buf = alloca(64);
                if (mtvp->name && mtvp->codec_name)
-                       snprintf(buf, 63, "%s (%s)",
-                                mtvp->name, mtvp->codec_name);
+                       write_fmt_str(printcharfun, "%s (%s)",
+                                     mtvp->name, mtvp->codec_name);
                else if (mtvp->name)
-                       snprintf(buf, 63, "%s [???]", mtvp->name);
+                       write_fmt_str(printcharfun, "%s [???]", mtvp->name);
                else if (mtvp->codec_name)
-                       snprintf(buf, 63, "??? (%s)", mtvp->codec_name);
-
-               write_c_string(buf, printcharfun);
+                       write_fmt_str(printcharfun, "??? (%s)", mtvp->codec_name);
        } else
                write_c_string("???", printcharfun);
 
-       if (mtvp->bitrate) {
-               char *buf = alloca(24);
-               snprintf(buf, 23, ", %d kb/s", mtvp->bitrate);
-               write_c_string(buf, printcharfun);
-       }
+       if (mtvp->bitrate)
+               write_fmt_str(printcharfun, ", %d kb/s", mtvp->bitrate);
 
        if (mtvp->width && mtvp->height) {
-               char *buf = alloca(48);
                if (mtvp->aspect_num > 1 && mtvp->aspect_den >= 1)
-                       snprintf(buf, 47, ", %dx%d (%d/%d)",
-                                mtvp->width, mtvp->height,
-                                mtvp->aspect_num, mtvp->aspect_den);
+                       write_fmt_str(printcharfun, ", %dx%d (%d/%d)",
+                                     mtvp->width, mtvp->height,
+                                     mtvp->aspect_num, mtvp->aspect_den);
                else
-                       snprintf(buf, 47, ", %dx%d (%.2f/1)",
-                                mtvp->width, mtvp->height,
-                                (double)mtvp->width/(double)mtvp->height);
-               write_c_string(buf, printcharfun);
+                       write_fmt_str(printcharfun, ", %dx%d (%.2f/1)",
+                                     mtvp->width, mtvp->height,
+                                     (double)mtvp->width/(double)mtvp->height);
        }
        write_c_string(">", printcharfun);
 }
index 34293de..c887ef0 100644 (file)
@@ -70,8 +70,6 @@ static void
 sound_alsa_print(Lisp_Object device, Lisp_Object pcfun, int ef)
 {
        sound_alsa_data_t *sad = NULL;
-       char *temp = alloca(48);
-
        sad = get_audio_device_data(device);
        /* cannot use incomplete or corrupt audio devices */
        if (XAUDIO_DEVICE_DRIVER(device) != MYSELF || sad == NULL) {
@@ -89,16 +87,12 @@ sound_alsa_print(Lisp_Object device, Lisp_Object pcfun, int ef)
                print_internal(sad->device, pcfun, ef);
 
        if (sad->keep_open) {
-               write_c_string(" :keep-open t", pcfun);
-               snprintf(temp, 47, " :handle 0x%lx",
-                        (long unsigned int)sad->handle);
-               write_c_string(temp, pcfun);
+               write_fmt_string(pcfun, " :keep-open t :handle 0x%lx",
+                                (long unsigned int)sad->handle);
        } else
                write_c_string(" :keep-open nil", pcfun);
 
-       snprintf(temp, 47, " :params 0x%lx", (long unsigned int)sad->hwparams);
-       write_c_string(temp, pcfun);
-
+       write_fmt_str(pcfun, " :params 0x%lx", (long unsigned int)sad->hwparams);
        return;
 }
 
index 0df7b42..ff57395 100644 (file)
@@ -71,9 +71,11 @@ Fixnum hp_play_gain;
 void player_error_internal(Audio * audio, char *text, long errorCode)
 {
        char errorbuff[132], buf[256];
+       int sz;
 
        AGetErrorText(audio, errorCode, errorbuff, 131);
-       sprintf(buf, "%s: %s\n", text, errorbuff);
+       sz = sprintf(buf, sizeof(buf), "%s: %s\n", text, errorbuff);
+       assert(sz>=0 && sz<sizeof(buf));
        error(buf);
 }
 
index 42699aa..8018e5f 100644 (file)
@@ -195,7 +195,8 @@ sound_jack_subthread_create(void)
        /* create out output ports */
        for (i = 0; i < sjsd->num_ports; i++) {
                char pname[30];
-               snprintf(pname, 30, "SXEmacs out_%d", i);
+               int sz = snprintf(pname, sizeof(pname), "SXEmacs out_%d", i);
+               assert(sz>=0 && sz<sizeof(pname));
                sjsd->ports[i] = jack_port_register(
                        client, pname,
                        JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
index ff73ea3..c2e1e18 100644 (file)
@@ -136,7 +136,6 @@ static void
 sound_pulse_print(Lisp_Object device, Lisp_Object pcfun, int ef)
 {
        sound_pulse_data_t spd = NULL;
-       char *temp = alloca(256);
        pa_context_state_t st;
 
        spd = get_audio_device_data(device);
@@ -209,17 +208,12 @@ sound_pulse_print(Lisp_Object device, Lisp_Object pcfun, int ef)
 
        write_c_string(" :api ", pcfun);
        if (spd->ml_threaded_p) {
-               write_c_string("#threaded", pcfun);
-               snprintf(temp, 255, " :mainloop 0x%lx",
-                        (long unsigned int)spd->tml);
+               write_fmt_str(pcfun, "#threaded :mainloop 0x%lx",
+                                (long unsigned int)spd->tml);
        } else {
-               write_c_string("#non-threaded", pcfun);
-               snprintf(temp, 255, " :mainloop 0x%lx",
-                        (long unsigned int)spd->ml);
+               write_fmt_str(pcfun, "#non-threaded :mainloop 0x%lx",
+                             (long unsigned int)spd->ml);
        }
-
-       write_c_string(temp, pcfun);
-
        return;
 }
 
index 715052e..e3a6c4f 100644 (file)
@@ -112,7 +112,7 @@ init_device(int volume, unsigned char *data, int fd,
                        char buf1[100], buf2[100], buf3[250];
                        audio_enc_to_str(&file_hdr, buf1);
                        audio_enc_to_str(&new_hdr, buf2);
-                       sprintf(buf3, "wanted %s, got %s", buf1, buf2);
+                       (void)snprintf(buf3, sizeof(buf3), "wanted %s, got %s", buf1, buf2);
                        warn(buf3);
                        return 1;
                }
@@ -120,8 +120,9 @@ init_device(int volume, unsigned char *data, int fd,
 
        if (volume < 0 || volume > 100) {
                char buf[255];
-               sprintf(buf, "volume must be between 0 and 100 (not %d)",
-                       volume);
+               int sz = sprintf(buf, sizeof(buf), "volume must be between 0 and 100 (not %d)",
+                                volume);
+               assert(sz>=0 && sz<sizeof(buf));
                warn(buf);
                return 1;
        }
@@ -198,8 +199,9 @@ void play_sound_file(char *sound_file, int volume)
                }
                if (wrtn != rrtn) {
                        char warn_buf[255];
-                       sprintf(warn_buf, "play: rrtn = %d, wrtn = %d", rrtn,
-                               wrtn);
+                       int sz = sprintf(warn_buf, sizeof(warn_buf), "play: rrtn = %d, wrtn = %d", rrtn,
+                                        wrtn);
+                       assert(warn_buf>=0 && warn_buf<sizeof(warn_buf));
                        warn(warn_buf);
                        goto END_OF_PLAY;
                }
@@ -271,7 +273,9 @@ int play_sound_data(unsigned char *data, int length, int volume)
        }
        if (wrtn != length) {
                char buf[255];
-               sprintf(buf, "play: rrtn = %d, wrtn = %d", length, wrtn);
+               int sz = snprintf(buf, sizeof(buf),
+                                 "play: rrtn = %d, wrtn = %d", length, wrtn);
+               assert(sz>=0 && sz < sizeof(buf));
                warn(buf);
                goto END_OF_PLAY;
        }
@@ -352,7 +356,7 @@ sound_native_audio_init(int audio_fd)
                        char buf1[100], buf2[100], buf3[250];
                        audio_enc_to_str(&file_hdr, buf1);
                        audio_enc_to_str(&new_hdr, buf2);
-                       sprintf(buf3, "wanted %s, got %s", buf1, buf2);
+                       (void)snprintf(buf3, sizeof(buf3), "wanted %s, got %s", buf1, buf2);
                        warn(buf3);
                        return 0;
                }
@@ -362,8 +366,10 @@ sound_native_audio_init(int audio_fd)
 #if 0
        if (volume < 0 || volume > 100) {
                char buf[255];
-               sprintf(buf, "volume must be between 0 and 100 (not %d)",
-                       volume);
+               int sz = snprintf(buf, sizeof(buf),
+                                 "volume must be between 0 and 100 (not %d)",
+                                 volume);
+               assert(sz>=0 && sz<sizeof(buf));
                warn(buf);
                return 0;
        }
index 18b2884..58789f6 100644 (file)
@@ -1003,7 +1003,7 @@ void float_to_string(char *buf, fpfloat data, int maxlen)
 #endif                         /* HAVE_FPFLOAT */
 
 /* Print NUMBER to BUFFER.
-   This is equivalent to sprintf (buffer, "%ld", number), only much faster.
+   This is equivalent to snprintf (buffer, maxlen, "%ld", number), only much faster.
 
    BUFFER should accept 24 bytes.  This should suffice for the longest
    numbers on 64-bit machines, including the `-' sign and the trailing
index 68305a2..4e345a5 100644 (file)
@@ -472,11 +472,13 @@ Lisp_Object make_process_internal(Lisp_Object name)
        /* If name is already in use, modify it until it is unused.  */
        name1 = name;
        for (i = 1;; i++) {
-               char suffix[10];
+               char suffix[24];
+               int sz;
                Lisp_Object tem = Fget_process(name1);
                if (NILP(tem))
                        break;
-               sprintf(suffix, "<%d>", i);
+               sz = snprintf(suffix, sizeof(suffix), "<%d>", i);
+               assert(sz>=0 && sz<sizeof(suffix));
                name1 = concat2(name, build_string(suffix));
        }
        name = name1;
index 374461a..93a4c30 100644 (file)
@@ -837,19 +837,22 @@ static GtkWidget *menu_descriptor_to_widget_1(Lisp_Object descr,
                if (!separator_string_p(XSTRING_DATA(name))) {
                        char *label_buffer = NULL;
                        char *temp_label = NULL;
+                       int sz, maxsz;
 
                        if (STRINGP(suffix) && XSTRING_LENGTH(suffix)) {
-                               label_buffer =
-                                   alloca(XSTRING_LENGTH(name) + 15 +
-                                          XSTRING_LENGTH(suffix));
-                               sprintf(label_buffer, "%s %s ",
-                                       XSTRING_DATA(name),
-                                       XSTRING_DATA(suffix));
+                               maxsz = XSTRING_LENGTH(name) + 15 +
+                                       XSTRING_LENGTH(suffix);
+                               label_buffer = alloca(maxsz);
+                               sz = snprintf(label_buffer, maxsz, "%s %s ",
+                                             XSTRING_DATA(name),
+                                             XSTRING_DATA(suffix));
+                               assert(sz>=0 && sz<maxsz);
                        } else {
-                               label_buffer =
-                                   alloca(XSTRING_LENGTH(name) + 15);
-                               sprintf(label_buffer, "%s ",
-                                       XSTRING_DATA(name));
+                               maxsz = XSTRING_LENGTH(name) + 15;
+                               label_buffer = alloca(maxsz);
+                               sz = sprintf(label_buffer, maxsz, "%s ",
+                                            XSTRING_DATA(name));
+                               assert(sz>=0 && sz<maxsz);
                        }
 
                        temp_label = convert_underscores(label_buffer);