unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: "Daniel Martín via Bug reports for GNU Emacs, the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
To: Alan Third <alan@idiocy.org>
Cc: 51105@debbugs.gnu.org, Eli Zaretskii <eliz@gnu.org>
Subject: bug#51105: 29.0.50; Buffer overflow bug in ns_compute_glyph_string_overhangs
Date: Sat, 09 Oct 2021 21:35:22 +0200	[thread overview]
Message-ID: <m1a6jirnyd.fsf@yahoo.es> (raw)
In-Reply-To: <YWGf1Bc+wFI3cixx@idiocy.org> (Alan Third's message of "Sat, 9 Oct 2021 14:57:40 +0100")

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

Alan Third <alan@idiocy.org> writes:

> On Sat, Oct 09, 2021 at 02:43:18PM +0300, Eli Zaretskii wrote:
>> > From: Daniel Martín <mardani29@yahoo.es>
>> > Cc: 51105@debbugs.gnu.org
>> > Date: Sat, 09 Oct 2021 12:06:36 +0200
>> > 
>> > Now I think that the right thing to do may be to modify nsterm.m, switch
>> > on the glyph type and, if the glyph type is COMPOSITE_GLYPH, call
>> > composition_gstring_width to get the glyph metrics.  Function
>> > composition_gstring_width uses the values from fields s->cmp_from and
>> > s->cmp_to, and would avoid the buffer overflow:
>> > 
>> > (lldb) fr v s->cmp_from
>> > (int) s->cmp_from = 6
>> > (lldb) fr v s->cmp_to
>> > (int) s->cmp_to = 7
>> > 
>> > WDYT? I can prepare a patch of this type if you agree.
>> 
>> SGTM, but I'd like to hear Alan's opinion as well, as I don't feel I
>> know enough about the NS display backend.
>
> I don't know much about this part of the code, but it sounds good to
> me too.

A reduced test case to reproduce the problem is to paste "العربية" in the
*scratch* buffer.

I've attached a patch that fixes the issue.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Fix-buffer-overflow-in-ns_compute_glyph_string_overh.patch --]
[-- Type: text/x-patch, Size: 2437 bytes --]

From 23897a25d7ddebc06ab855058d36a5e291e5cba3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Daniel=20Mart=C3=ADn?= <mardani29@yahoo.es>
Date: Sat, 9 Oct 2021 21:10:20 +0200
Subject: [PATCH] Fix buffer overflow in ns_compute_glyph_string_overhangs

* src/nsterm.m (ns_compute_glyph_string_overhangs): When the first
glyph of a glyph string is a composite glyph, `s->nchars' is 0, so
"s->char2b + s->nchars - 1" dereferenced a position before buffer
`s->char2b'.  Instead, rewrite part of the function to distinguish
between character glyphs and composite glyphs.  For character glyphs,
calculate the font metrics using the `text_extents' function, passing
it the entire glyph string; for composite glyphs, call
`composition_gstring_width'. (Bug#51105)
---
 src/nsterm.m | 29 ++++++++++++++++++-----------
 1 file changed, 18 insertions(+), 11 deletions(-)

diff --git a/src/nsterm.m b/src/nsterm.m
index a6c2e7505b..e616766ec7 100644
--- a/src/nsterm.m
+++ b/src/nsterm.m
@@ -2848,20 +2848,27 @@ Hide the window (X11 semantics)
      External (RIF); compute left/right overhang of whole string and set in s
    -------------------------------------------------------------------------- */
 {
-  struct font *font = s->font;
-
   if (s->char2b)
     {
       struct font_metrics metrics;
-      unsigned int codes[2];
-      codes[0] = *(s->char2b);
-      codes[1] = *(s->char2b + s->nchars - 1);
-
-      font->driver->text_extents (font, codes, 2, &metrics);
-      s->left_overhang = -metrics.lbearing;
-      s->right_overhang
-	= metrics.rbearing > metrics.width
-	? metrics.rbearing - metrics.width : 0;
+      if (s->first_glyph->type == CHAR_GLYPH && !s->font_not_found_p)
+        {
+          struct font *font = s->font;
+          font->driver->text_extents (font, s->char2b, s->nchars, &metrics);
+          s->left_overhang = -metrics.lbearing;
+          s->right_overhang
+            = metrics.rbearing > metrics.width
+            ? metrics.rbearing - metrics.width : 0;
+        }
+      else if (s->first_glyph->type == COMPOSITE_GLYPH)
+        {
+          Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
+
+	  composition_gstring_width (gstring, s->cmp_from, s->cmp_to, &metrics);
+	  s->right_overhang = (metrics.rbearing > metrics.width
+			       ? metrics.rbearing - metrics.width : 0);
+	  s->left_overhang = metrics.lbearing < 0 ? -metrics.lbearing : 0;
+        }
     }
   else
     {
-- 
2.31.0


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


Let me know if you like it and please install it on my behalf if so.
Thanks.


  reply	other threads:[~2021-10-09 19:35 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <m17den59au.fsf.ref@yahoo.es>
2021-10-09  0:30 ` bug#51105: 29.0.50; Buffer overflow bug in ns_compute_glyph_string_overhangs Daniel Martín via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-10-09  6:40   ` Eli Zaretskii
2021-10-09 10:06     ` Daniel Martín via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-10-09 11:43       ` Eli Zaretskii
2021-10-09 13:57         ` Alan Third
2021-10-09 19:35           ` Daniel Martín via Bug reports for GNU Emacs, the Swiss army knife of text editors [this message]
2021-10-09 19:41             ` Daniel Martín via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-11-05  2:39               ` Lars Ingebrigtsen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m1a6jirnyd.fsf@yahoo.es \
    --to=bug-gnu-emacs@gnu.org \
    --cc=51105@debbugs.gnu.org \
    --cc=alan@idiocy.org \
    --cc=eliz@gnu.org \
    --cc=mardani29@yahoo.es \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).