unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#8435: misuse of error ("...%d...", ...) on 64-bit hosts
@ 2011-04-06 19:59 Paul Eggert
       [not found] ` <handler.8435.B.130211997010029.ack@debbugs.gnu.org>
  0 siblings, 1 reply; 22+ messages in thread
From: Paul Eggert @ 2011-04-06 19:59 UTC (permalink / raw)
  To: 8435

In the Emacs trunk, src/dispnew.c contains this:

    error ("Device %d is not a termcap terminal device", t->id);

t->id is of type 'int', but the "error" routine formats %d as if it
were of type EMACS_INT.  This works on a typical 32-bit machine, but
on a 64-bit machine these two types have different representations,
and the above call relies on undefined behavior: it might work and it
might not.

The above bug can easily be fixed by casting t->id to EMACS_INT, but
other instances of the problem are not so easy.  For example,
src/term.c has this:

    maybe_fatal (must_succeed, terminal,
                 "Screen size %dx%d is too small",
                 "Screen size %dx%d is too small",
                 FrameCols (tty), FrameRows (tty));

where FrameCols and FrameRows return 'int'.  Here, if MUST_SUCCEED is
true, maybe_fatal calls 'printf' and works; but if MUST_SUCCEED is
false, maybe_fatal calls 'error' and might not work on a 64-bit machine.

I found these bugs by code inspection, and expect that there are
others like them.  Part of the problem is that it's confusing that
'error' treats format strings differently from 'printf'.  And partly
the problem is that there is currently no reliable way to catch common
programming mistakes like this.

I plan to fix this problem systematically, as follows.

  * Provide a convenient way to format EMACS_INT values using
    printf-like functions.

  * Change 'error' and similar functions so that they use
    printf-compatible format strings, and change their callers to
    format EMACS_INT values accordingly.

  * Mark 'error'-like functions with ATTRIBUTE_FORMAT_PRINTF, so that
    we can easily find other bugs like the above.






^ permalink raw reply	[flat|nested] 22+ messages in thread

end of thread, other threads:[~2011-04-25  6:42 UTC | newest]

Thread overview: 22+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-04-06 19:59 bug#8435: misuse of error ("...%d...", ...) on 64-bit hosts Paul Eggert
     [not found] ` <handler.8435.B.130211997010029.ack@debbugs.gnu.org>
2011-04-07  7:33   ` Paul Eggert
2011-04-07 12:29     ` Andy Moreton
2011-04-07 18:31     ` Eli Zaretskii
2011-04-07 20:43       ` Paul Eggert
2011-04-08  8:58         ` Eli Zaretskii
2011-04-08 23:34           ` Paul Eggert
2011-04-09  7:20             ` Eli Zaretskii
2011-04-09 18:24               ` Paul Eggert
2011-04-09 18:32                 ` Eli Zaretskii
2011-04-09 19:39                   ` Paul Eggert
2011-04-09 20:21                     ` Eli Zaretskii
2011-04-10  3:52                       ` Stefan Monnier
2011-04-10  5:19                         ` Eli Zaretskii
2011-04-11  1:44                           ` Stefan Monnier
2011-04-11  2:54                             ` Eli Zaretskii
2011-04-10 17:03                       ` Paul Eggert
2011-04-09 19:28                 ` Eli Zaretskii
2011-04-09 19:43                   ` Paul Eggert
2011-04-08 23:37           ` Paul Eggert
2011-04-25  0:59             ` Daniel Colascione
2011-04-25  6:42               ` Eli Zaretskii

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).