GNU bug report logs - #20783
25.0.50; [PATCH] byte-to-position has internal off-by-one bug

Previous Next

Package: emacs;

Reported by: Wolfgang Jenkner <wjenkner <at> inode.at>

Date: Wed, 10 Jun 2015 15:20:05 UTC

Severity: normal

Tags: patch

Found in version 25.0.50

Fixed in version 25.1

Done: Wolfgang Jenkner <wjenkner <at> inode.at>

Bug is archived. No further changes may be made.

Full log


Message #14 received at 20783 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: Wolfgang Jenkner <wjenkner <at> inode.at>
Cc: 20783 <at> debbugs.gnu.org
Subject: Re: bug#20783: 25.0.50;
 [PATCH] byte-to-position has internal off-by-one bug
Date: Thu, 11 Jun 2015 19:04:24 +0300
> From: Wolfgang Jenkner <wjenkner <at> inode.at>
> Cc: 20783 <at> debbugs.gnu.org
> Date: Thu, 11 Jun 2015 17:24:42 +0200
> 
> I see.  How about something like the patch below?  The loop could be
> improved a bit by doing pointer arithmetic like in DEC_POS but it's
> perhaps not worth complicating things for the case where bytepos is not
> at a character boundary.
> 
> -- >8 --
> Subject: [PATCH] * editfns.c (Fbyte_to_position): Fix bytepos not at char
>  boundary.
> 
> The behavior now matches the description in the manual.  (Bug#20783)
> ---
>  src/editfns.c | 12 ++++++++++--
>  1 file changed, 10 insertions(+), 2 deletions(-)
> 
> diff --git a/src/editfns.c b/src/editfns.c
> index cddb0d4..94715fe 100644
> --- a/src/editfns.c
> +++ b/src/editfns.c
> @@ -1025,10 +1025,18 @@ DEFUN ("byte-to-position", Fbyte_to_position, Sbyte_to_position, 1, 1, 0,
>  If BYTEPOS is out of range, the value is nil.  */)
>    (Lisp_Object bytepos)
>  {
> +  ptrdiff_t pos_byte;
> +
>    CHECK_NUMBER (bytepos);
> -  if (XINT (bytepos) < BEG_BYTE || XINT (bytepos) > Z_BYTE)
> +  pos_byte = XINT (bytepos);
> +  if (pos_byte < BEG_BYTE || pos_byte > Z_BYTE)
>      return Qnil;
> -  return make_number (BYTE_TO_CHAR (XINT (bytepos)));
> +  if (Z != Z_BYTE)
> +    /* There are multibyte characters in the buffer.
> +       Search for the start of the current character. */
> +    while (!CHAR_HEAD_P (FETCH_BYTE (pos_byte)))
> +      pos_byte--;
> +  return make_number (BYTE_TO_CHAR (pos_byte));
>  }

Works for me, thanks.  But please add a comment there about
BYTE_TO_CHAR expecting byte positions that are on a character
boundary, so that the reason for the loop is clear.




This bug report was last modified 10 years and 36 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.