zsh-workers
 help / color / mirror / code / Atom feed
From: Oliver Kiddle <opk@zsh.org>
To: zsh-workers@zsh.org
Subject: metafy() (was Re: $watch, log and Cyrillic usernames)
Date: Sun, 08 Oct 2023 23:47:25 +0200	[thread overview]
Message-ID: <53125-1696801645.349232@KWzA.NFGU.Vwsy> (raw)
In-Reply-To: <51326-1696644317.959346@Oqom.p2Z_.86FS>

I wrote:
> However, in looking closer at the code I observed the existing use of
> sizeof(u->ut_name) which is 32 on my system. So I tried creating 32 and
> 33 character usernames (which, incidentally, useradd was happy with) and

It occurred to be to double check what metafy() does about null
termination with META_USEHEAP. The comment by META_USEHEAP is "get
memory from the heap. This leaves buf unchanged". The main way it
differs from META_HEAPDUP is that if no characters that need metafying
are found, it will return back the original passed buf. However, it does
add a terminating null at the len + 1 position so while the buf pointer is
unchanged, what it points to does get changed.

There aren't especially many calls to metafy with META_USEHEAP and in
most cases, the call uses the result of getkeystring(). I
noticed one case where we do need to add 1 byte to an allocation to
accomodate this null.

I'm not sure what the best approach is for the watch module. Subtracting 1
from n in each call to strnlen() avoids writing a null past the end of
the buffer but is not ideal for 32 character usernames. Using
META_HEAPDUP instead means a lot of heap allocations in the normal case
where there are only short ASCII-only usernames. Any ideas?

Oliver

diff --git a/Src/subst.c b/Src/subst.c
index cdbfc138a..60d850feb 100644
--- a/Src/subst.c
+++ b/Src/subst.c
@@ -1506,7 +1506,7 @@ substevalchar(char *ptr)
     }
 #ifdef MULTIBYTE_SUPPORT
     else if (isset(MULTIBYTE) && ires > 127) {
-	ptr = zhalloc(MB_CUR_MAX);
+	ptr = zhalloc(MB_CUR_MAX+1);
 	len = ucs4tomb((unsigned int)ires & 0xffffffff, ptr);
     }
     if (len <= 0)


  parent reply	other threads:[~2023-10-08 21:48 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-05 21:29 $watch, log and Cyrillic usernames Максим
2023-10-05 21:42 ` Bart Schaefer
2023-10-07  2:05 ` Oliver Kiddle
2023-10-07 16:49   ` Максим
2023-10-08  0:24     ` Oliver Kiddle
2023-10-08  6:20       ` Максим
2023-10-08 21:47   ` Oliver Kiddle [this message]
2023-10-09  2:01     ` metafy() (was Re: $watch, log and Cyrillic usernames) Bart Schaefer
2023-10-10 21:46       ` Oliver Kiddle

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

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

  git send-email \
    --in-reply-to=53125-1696801645.349232@KWzA.NFGU.Vwsy \
    --to=opk@zsh.org \
    --cc=zsh-workers@zsh.org \
    /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.vuxu.org/mirror/zsh/

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 NNTP newsgroup(s).