unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#29609: 27.0.50; Format number as hexadecimal with hash-flag
@ 2017-12-07 21:02 Gustaf Waldemarson
  2017-12-08  2:14 ` Paul Eggert
  0 siblings, 1 reply; 4+ messages in thread
From: Gustaf Waldemarson @ 2017-12-07 21:02 UTC (permalink / raw)
  To: 29609

Hello,

I recently noticed that the `format` function behaves incorrectly when
formatting numbers as hexadecimal together with the hash-flag. E.g.,
following this recipe yields:

1. M-x ielm
2. Evaluate: (format "%#08x" #x10) -> "00000x10"

Typically Bash, C and other formatting implementations typically formats
this as "0x00000010", and I think Emacs should do the same in this case.

This seems to be present in a few different versions of Emacs, I've
tried it on 24.5.1 and 27.0.50 and they both behave the same.

Regards,
Gustaf


In GNU Emacs 27.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.22.11)
 of 2017-11-29 built on ShadowX
Repository revision: 95369ac346d75a7b75bc2c5adf632cc4faa1241a
Windowing system distributor 'The X.Org Foundation', version 11.0.11903000
System Description:    Ubuntu 17.04

Recent messages:
scroll-down-command: Beginning of buffer
Mark set (rectangle mode)
When done with a buffer, type C-x #
Configuring package message...done
Configuring package git-commit...done
Configuring package magit...done [2 times]
When done with a buffer, type C-x # [2 times]
Making completion list... [2 times]
delete-backward-char: Text is read-only
Making completion list...

Configured using:
 'configure --prefix=/home/xaldew/.local
 '--program-transform-name=s/^ctags$/ctags.emacs/''

Configured features:
XPM JPEG TIFF GIF PNG RSVG IMAGEMAGICK SOUND DBUS GSETTINGS NOTIFY
GNUTLS LIBXML2 FREETYPE XFT ZLIB TOOLKIT_SCROLL_BARS GTK3 X11 LCMS2

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

Major mode: IELM

Minor modes in effect:
  diff-auto-refine-mode: t
  global-git-commit-mode: t
  async-bytecomp-package-mode: t
  beacon-mode: t
  flycheck-pos-tip-mode: t
  projectile-mode: t
  hes-mode: t
  global-linum-mode: t
  linum-mode: t
  yas-global-mode: t
  yas-minor-mode: t
  global-company-mode: t
  company-mode: t
  global-anzu-mode: t
  anzu-mode: t
  perspeen-mode: t
  global-atomic-chrome-edit-mode: t
  shell-dirtrack-mode: t
  global-undo-tree-mode: t
  undo-tree-mode: t
  which-key-mode: t
  anyclip-mode: t
  override-global-mode: t
  electric-pair-mode: t
  save-place-mode: t
  global-subword-mode: t
  subword-mode: t
  show-paren-mode: t
  winner-mode: t
  global-auto-revert-mode: t
  xterm-mouse-mode: t
  savehist-mode: t
  ido-everywhere: t
  tooltip-mode: t
  global-eldoc-mode: t
  eldoc-mode: t
  electric-indent-mode: t
  mouse-wheel-mode: t
  tool-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t
  column-number-mode: t
  line-number-mode: t
  transient-mark-mode: t

Load-path shadows:
None found.

Features:
(shadow sort bbdb-message mail-extr emacsbug sendmail ielm pp
magit-obsolete magit-blame magit-stash magit-bisect magit-remote
magit-commit magit-sequence magit-notes magit-worktree magit-branch
magit-files magit-refs magit-status magit magit-repos magit-apply
magit-wip magit-log magit-diff smerge-mode diff-mode magit-core
magit-autorevert magit-process magit-margin magit-mode magit-git
magit-section magit-popup git-commit magit-utils crm log-edit message
rmc puny rfc822 mml mml-sec epa derived epg mm-decode mm-bodies
mm-encode mail-parse rfc2231 mailabbrev gmm-utils mailheader pcvs-util
add-log with-editor async-bytecomp async unfill org-rmail org-mhe
org-irc org-info org-gnus gnus-util rmail rmail-loaddefs rfc2047 rfc2045
ietf-drums mm-util mail-prsvr mail-utils org-docview doc-view jka-compr
image+ image-file image-mode org-bibtex bibtex org-bbdb org-w3m
org-table org-element avl-tree generator ob-latex ob-plantuml ob-org
ob-shell ob-gnuplot ob-python ob-ditaa ob-dot org org-macro org-footnote
org-pcomplete org-list org-faces org-entities org-version ob-emacs-lisp
ob ob-tangle org-src ob-ref ob-lob ob-table ob-keys ob-exp ob-comint
ob-core ob-eval org-compat org-macs org-loaddefs holidays hol-loaddefs
cal-menu calendar cal-loaddefs markdown-mode noutline outline dired
dired-loaddefs term/tmux term/xterm xterm smart-mode-line-dark-theme
smart-mode-line rich-minority beacon server flymake-proc flymake
warnings auto-complete-config auto-complete form-feed paredit nameless
lisp-mnt flyspell ispell whitespace flycheck-irony irony-diagnostics
irony irony-iotask flycheck-popup-tip popup flycheck-pos-tip pos-tip
flycheck find-func subr-x dash rainbow-delimiters projectile grep
ibuf-ext ibuffer ibuffer-loaddefs ggtags compile ewoc
highlight-escape-sequences linum yasnippet company-oddmuse
company-keywords company-etags etags xref project company-gtags
company-dabbrev-code company-dabbrev company-files company-capf
company-cmake company-xcode company-clang company-semantic company-eclim
company-css company-nxml company-bbdb company-template company pcase
anzu thingatpt perspeen perspeen-tab powerline powerline-separators
powerline-themes atomic-chrome websocket url-cookie url-domsuf url-util
bindat let-alist json map color-theme-approximate color tramp
tramp-compat tramp-loaddefs trampver ucs-normalize shell pcomplete
comint ansi-color parse-time format-spec delim-col hydra-examples
windmove rect hydra lv bbdb bbdb-site timezone cus-edit cus-start
cus-load wid-edit ace-link avy google-c-style undo-tree diff cl-extra
help-mode which-key advice anyclip-mode diminish use-package bind-key
easy-mmode finder-inf autoinsert tex-site edmacro kmacro rx info package
epg-config url-handlers url-parse auth-source cl-seq eieio eieio-core
cl-macs eieio-loaddefs password-cache url-vars local-autoloads cwarn
cc-mode cc-fonts easymenu cc-guess cc-menus cc-cmds cc-styles cc-align
cc-engine cc-vars cc-defs elec-pair saveplace cap-words superword
subword time-date paren winner ring autorevert filenotify xt-mouse
tango-dark-theme savehist ido seq byte-opt gv bytecomp byte-compile
cconv cl-loaddefs cl-lib keybinds terminals mule-util tooltip eldoc
electric uniquify ediff-hook vc-hooks lisp-float-type 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 elisp-mode lisp-mode
prog-mode register page menu-bar rfn-eshadow isearch timer select
scroll-bar mouse jit-lock font-lock syntax facemenu font-core
term/tty-colors frame 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 charscript charprop case-table epa-hook jka-cmpr-hook
help simple abbrev obarray minibuffer cl-preloaded nadvice loaddefs
button faces cus-face macroexp files text-properties overlay sha1 md5
base64 format env code-pages mule custom widget hashtable-print-readable
backquote dbusbind inotify lcms2 dynamic-setting system-font-setting
font-render-setting move-toolbar gtk x-toolkit x multi-tty
make-network-process emacs)

Memory information:
((conses 16 546994 37401)
 (symbols 48 49359 3)
 (miscs 40 1210 530)
 (strings 32 135385 7494)
 (string-bytes 1 4760808)
 (vectors 16 81794)
 (vector-slots 8 1291186 11024)
 (floats 8 690 550)
 (intervals 56 2095 794)
 (buffers 992 109))





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

* bug#29609: 27.0.50; Format number as hexadecimal with hash-flag
  2017-12-07 21:02 bug#29609: 27.0.50; Format number as hexadecimal with hash-flag Gustaf Waldemarson
@ 2017-12-08  2:14 ` Paul Eggert
  2017-12-08 17:51   ` Glenn Morris
  0 siblings, 1 reply; 4+ messages in thread
From: Paul Eggert @ 2017-12-08  2:14 UTC (permalink / raw)
  To: Gustaf Waldemarson; +Cc: 29609-done

[-- Attachment #1: Type: text/plain, Size: 79 bytes --]

Thanks for reporting that. I installed the attached, which should fix the bug.

[-- Attachment #2: 0001-Fix-zero-padding-bug-with-format-08x-n.patch --]
[-- Type: text/x-patch, Size: 2700 bytes --]

From 47423f0603f2ecfb78352be5477fb02c44f1fd35 Mon Sep 17 00:00:00 2001
From: Paul Eggert <eggert@cs.ucla.edu>
Date: Thu, 7 Dec 2017 18:07:54 -0800
Subject: [PATCH] Fix zero-padding bug with (format "%#08x" n)

Problem reported by Gustaf Waldemarson (Bug#29609).
* src/editfns.c (styled_format):
Put zero padding after a leading "0x", not before.
* test/src/editfns-tests.el (format-sharp-0-x): New test.
---
 src/editfns.c             | 24 ++++++++++++++++--------
 test/src/editfns-tests.el |  6 ++++++
 2 files changed, 22 insertions(+), 8 deletions(-)

diff --git a/src/editfns.c b/src/editfns.c
index e671ba0..ebf6518 100644
--- a/src/editfns.c
+++ b/src/editfns.c
@@ -4722,11 +4722,19 @@ styled_format (ptrdiff_t nargs, Lisp_Object *args, bool message)
 		  char src0 = src[0];
 		  int exponent_bytes = 0;
 		  bool signedp = src0 == '-' || src0 == '+' || src0 == ' ';
-		  unsigned char after_sign = src[signedp];
-		  if (zero_flag && 0 <= char_hexdigit (after_sign))
+		  int prefix_bytes = (signedp
+				      + ((src[signedp] == '0'
+					  && (src[signedp + 1] == 'x'
+					      || src[signedp + 1] == 'X'))
+					 ? 2 : 0));
+		  if (zero_flag)
 		    {
-		      leading_zeros += padding;
-		      padding = 0;
+		      unsigned char after_prefix = src[prefix_bytes];
+		      if (0 <= char_hexdigit (after_prefix))
+			{
+			  leading_zeros += padding;
+			  padding = 0;
+			}
 		    }
 
 		  if (excess_precision
@@ -4745,13 +4753,13 @@ styled_format (ptrdiff_t nargs, Lisp_Object *args, bool message)
 		      nchars += padding;
 		    }
 
-		  *p = src0;
-		  src += signedp;
-		  p += signedp;
+		  memcpy (p, src, prefix_bytes);
+		  p += prefix_bytes;
+		  src += prefix_bytes;
 		  memset (p, '0', leading_zeros);
 		  p += leading_zeros;
 		  int significand_bytes
-		    = sprintf_bytes - signedp - exponent_bytes;
+		    = sprintf_bytes - prefix_bytes - exponent_bytes;
 		  memcpy (p, src, significand_bytes);
                   p += significand_bytes;
 		  src += significand_bytes;
diff --git a/test/src/editfns-tests.el b/test/src/editfns-tests.el
index 70dc937..283a642 100644
--- a/test/src/editfns-tests.el
+++ b/test/src/editfns-tests.el
@@ -136,6 +136,12 @@ transpose-test-get-byte-positions
 (ert-deftest format-c-float ()
   (should-error (format "%c" 0.5)))
 
+;;; Test for Bug#29609.
+(ert-deftest format-sharp-0-x ()
+  (should (string-equal (format "%#08x" #x10) "0x000010"))
+  (should (string-equal (format "%#05X" #x10) "0X010"))
+  (should (string-equal (format "%#04x" 0) "0000")))
+
 ;;; Check format-time-string with various TZ settings.
 ;;; Use only POSIX-compatible TZ values, since the tests should work
 ;;; even if tzdb is not in use.
-- 
2.7.4


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

* bug#29609: 27.0.50; Format number as hexadecimal with hash-flag
  2017-12-08  2:14 ` Paul Eggert
@ 2017-12-08 17:51   ` Glenn Morris
  2017-12-08 21:31     ` Paul Eggert
  0 siblings, 1 reply; 4+ messages in thread
From: Glenn Morris @ 2017-12-08 17:51 UTC (permalink / raw)
  To: 29609; +Cc: eggert, gustaf.waldemarson


Since 47423f0, three bootstraps in a row have failed for me on RHEL7.4,
with a segfault at some random point in the build. Eg:
    
    /bin/sh: line 1: 26588 Segmentation fault      (core dumped)
    EMACSLOADPATH= '../src/emacs' -batch --no-site-file --no-site-lisp
     --eval '(setq load-prefer-newer t)' -f batch-byte-compile
    calc/calc-keypd.el
    make[2]: *** [calc/calc-keypd.elc] Error 139

Could be a coincidence, but before this change I haven't had such
failures. Could there be something wrong with 47423f0?





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

* bug#29609: 27.0.50; Format number as hexadecimal with hash-flag
  2017-12-08 17:51   ` Glenn Morris
@ 2017-12-08 21:31     ` Paul Eggert
  0 siblings, 0 replies; 4+ messages in thread
From: Paul Eggert @ 2017-12-08 21:31 UTC (permalink / raw)
  To: Glenn Morris, 29609; +Cc: gustaf.waldemarson

[-- Attachment #1: Type: text/plain, Size: 203 bytes --]

On 12/08/2017 09:51 AM, Glenn Morris wrote:
> Could there be something wrong with 47423f0?

Yes there could, and thanks for reporting it. I installed the attached 
to fix the bug I recently introduced.


[-- Attachment #2: 0001-Fix-core-dump-from-format-08x-n-patch.patch --]
[-- Type: text/x-patch, Size: 859 bytes --]

From c9d450ffcc755c4af39c2ab5a9e585f05eeddf94 Mon Sep 17 00:00:00 2001
From: Paul Eggert <eggert@cs.ucla.edu>
Date: Fri, 8 Dec 2017 13:30:06 -0800
Subject: [PATCH] Fix core dump from (format "%#08x" n) patch

Problem reported by Glenn Morris (Bug#29609#13).
* src/editfns.c (styled_format): Null-terminate output,
as later code now expects this.
---
 src/editfns.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/src/editfns.c b/src/editfns.c
index ebf6518994..084d92346f 100644
--- a/src/editfns.c
+++ b/src/editfns.c
@@ -4623,6 +4623,7 @@ styled_format (ptrdiff_t nargs, Lisp_Object *args, bool message)
 		  /* Don't use sprintf here, as it might mishandle prec.  */
 		  sprintf_buf[0] = XINT (arg);
 		  sprintf_bytes = prec != 0;
+		  sprintf_buf[sprintf_bytes] = '\0';
 		}
 	      else if (conversion == 'd' || conversion == 'i')
 		{
-- 
2.14.3


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

end of thread, other threads:[~2017-12-08 21:31 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-12-07 21:02 bug#29609: 27.0.50; Format number as hexadecimal with hash-flag Gustaf Waldemarson
2017-12-08  2:14 ` Paul Eggert
2017-12-08 17:51   ` Glenn Morris
2017-12-08 21:31     ` Paul Eggert

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).