zsh-workers
 help / color / mirror / code / Atom feed
From: Mikael Magnusson <mikachu@gmail.com>
To: zsh workers <zsh-workers@zsh.org>
Subject: [PATCH 3/4] attr: dynamically allocate memory for attributes
Date: Tue, 3 Nov 2009 19:57:06 +0100 (CET)	[thread overview]
Message-ID: <alpine.LNX.2.00.0911031956240.3783@localhost> (raw)
In-Reply-To: <alpine.LNX.2.00.0911031951480.3519@localhost>

If there is a race condition and the attribute grows between the two
calls, we return an error instead of trying again.
---
  Doc/Zsh/mod_attr.yo |    5 ++++
  Src/Modules/attr.c  |   63 ++++++++++++++++++++++++++++++++++-----------------
  2 files changed, 47 insertions(+), 21 deletions(-)

diff --git a/Doc/Zsh/mod_attr.yo b/Doc/Zsh/mod_attr.yo
index ed444d0..517bb63 100644
--- a/Doc/Zsh/mod_attr.yo
+++ b/Doc/Zsh/mod_attr.yo
@@ -32,3 +32,8 @@ var(filename). If the optional argument var(parameter) is given, the
  list of attributes is set on that parameter instead of being printed to stdout.
  )
  enditem()
+
+tt(zgetattr) and tt(zlistattr) allocate memory dynamically. If the attribute or
+list of attributes grows between the allocation and the call to get them, they
+return 2. On all other errors, 1 is returned. This way, the calling function can
+check for this case and try again for as long as they want.
diff --git a/Src/Modules/attr.c b/Src/Modules/attr.c
index 1a9c323..bb30ebb 100644
--- a/Src/Modules/attr.c
+++ b/Src/Modules/attr.c
@@ -98,26 +98,37 @@ static int
  bin_getattr(char *nam, char **argv, Options ops, UNUSED(int func))
  {
      int ret = 0;
-    int len, slen;
-    char value[256];
+    int list_len, val_len, attr_len, slen;
+    char *value;
      int symlink = OPT_ISSET(ops, 'h');

      unmetafy(*argv, &slen);
      unmetafy(*(argv+1), NULL);
-    if (xlistxattr(*argv, NULL, 0, symlink) > 0) {
-        if (0 < (len = xgetxattr(*argv, *(argv+1), value, 255, symlink))) {
-            if (len < 256) {
-                value[len] = '\0';
+    list_len = xlistxattr(*argv, NULL, 0, symlink);
+    if (list_len > 0) {
+        val_len = xgetxattr(*argv, *(argv+1), NULL, 0, symlink);
+        if (val_len == 0) {
+            if (*(argv+2))
+                unsetparam(*(argv+2));
+            return 0;
+        }
+        if (val_len > 0) {
+            value = (char *)zalloc(val_len+1);
+            attr_len = xgetxattr(*argv, *(argv+1), value, val_len, symlink);
+            if (attr_len > 0 && attr_len <= val_len) {
+                value[attr_len] = '\0';
                  if (*(argv+2))
-                    setsparam(*(argv+2), metafy(value, len, META_DUP));
+                    setsparam(*(argv+2), metafy(value, attr_len, META_DUP));
                  else
                      printf("%s\n", value);
              }
-        } else if (len < 0) {
-            zwarnnam(nam, "%s: %e", metafy(*argv, slen, META_NOALLOC), errno);
-            ret = 1;
+            zfree(value, val_len+1);
          }
      }
+    if (list_len < 0 || val_len < 0 || attr_len < 0)  {
+        zwarnnam(nam, "%s: %e", metafy(*argv, slen, META_NOALLOC), errno);
+        ret = 1 + (attr_len > val_len);
+    }
      return ret;
  }

@@ -160,41 +171,51 @@ static int
  bin_listattr(char *nam, char **argv, Options ops, UNUSED(int func))
  {
      int ret = 0;
-    int len, slen;
-    char value[256];
+    int val_len, list_len, slen;
+    char *value;
      int symlink = OPT_ISSET(ops, 'h');

      unmetafy(*argv, &slen);
-    if (0 < (len = xlistxattr(*argv, value, 256, symlink))) {
-        if (len < 256) {
+    val_len = xlistxattr(*argv, NULL, 0, symlink);
+    if (val_len == 0) {
+        if (*(argv+1))
+            unsetparam(*(argv+1));
+        return 0;
+    }
+    if (val_len > 0) {
+        value = (char *)zalloc(val_len+1);
+        list_len = xlistxattr(*argv, value, val_len, symlink);
+        if (list_len > 0 && list_len <= val_len) {
              char *p = value;
              if (*(argv+1)) {
-                if (strlen(value) + 1 == len)
-                    setsparam(*(argv+1), metafy(value, len-1, META_DUP));
+                if (strlen(value) + 1 == list_len)
+                    setsparam(*(argv+1), metafy(value, list_len-1, META_DUP));
                  else {
                      int arrlen = 0;
                      char **array = NULL, **arrptr = NULL;

-                    while (p < &value[len]) {
+                    while (p < &value[list_len]) {
                          arrlen++;
                          p += strlen(p) + 1;
                      }
                      arrptr = array = (char **)zshcalloc((arrlen+1) * sizeof(char *));
                      p = value;
-                    while (p < &value[len]) {
+                    while (p < &value[list_len]) {
                          *arrptr++ = metafy(p, -1, META_DUP);
                          p += strlen(p) + 1;
                      }
                      setaparam(*(argv+1), array);
                  }
-            } else while (p < &value[len]) {
+            } else while (p < &value[list_len]) {
                  printf("%s\n", p);
                  p += strlen(p) + 1;
              }
          }
-    } else if (len < 0) {
+        zfree(value, val_len+1);
+    }
+    if (val_len < 0 || list_len < 0) {
          zwarnnam(nam, "%s: %e", metafy(*argv, slen, META_NOALLOC), errno);
-        ret = 1;
+        ret = 1 + (list_len > val_len);
      }
      return ret;
  }
-- 
1.6.5


  parent reply	other threads:[~2009-11-03 19:04 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-02-26 21:55 [wip patch] new zsh/attr module Mikael Magnusson
2009-02-26 22:36 ` Mikael Magnusson
2009-02-27  0:48 ` Mikael Magnusson
2009-03-03 12:12 ` Peter Stephenson
2009-03-03 14:43   ` Mikael Magnusson
2009-03-03 16:35     ` Peter Stephenson
2009-03-03 16:51       ` Mikael Magnusson
2009-03-03 16:55         ` Peter Stephenson
2009-03-03 17:00           ` Mikael Magnusson
2009-11-03 18:52         ` Mikael Magnusson
2009-11-03 18:55           ` [PATCH 1/4] attr: add -h option to operate on symlinks without dereferencing Mikael Magnusson
2009-11-04 10:48             ` Peter Stephenson
2009-11-04 11:01               ` Mikael Magnusson
2009-11-04 11:36                 ` Peter Stephenson
2010-09-12 11:12             ` Mikael Magnusson
2009-11-03 18:56           ` [PATCH 2/4] attr: Make zlistattr return an array, zdelattr take several attrs Mikael Magnusson
2009-11-05  6:51             ` [PATCH 2/4] attr: Make zlistattr return an array, zdelattr takeseveral attrs Jun T.
2009-11-05  9:48               ` Mikael Magnusson
2009-11-03 18:57           ` Mikael Magnusson [this message]
2009-11-03 18:57           ` [PATCH 4/4] attr: Use descriptive variables for argv and allow setting values with embedded nuls Mikael Magnusson

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=alpine.LNX.2.00.0911031956240.3783@localhost \
    --to=mikachu@gmail.com \
    --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).