zsh-workers
 help / color / mirror / code / Atom feed
From: "Jun. T" <takimoto-j@kba.biglobe.ne.jp>
To: zsh-workers@zsh.org
Subject: Re: [PATCHv1] [long] improvements to limit/ulimit API and doc
Date: Sat, 28 Nov 2020 03:24:04 +0900	[thread overview]
Message-ID: <FF7E5A4B-E00F-4160-AFBD-45783C0BAF7B@kba.biglobe.ne.jp> (raw)
In-Reply-To: <20201126172354.g5guqrhu3mwrv6ia@chazelas.org>


> 2020/11/27 2:23, Stephane Chazelas <stephane@chazelas.org> wrote:
> 
> 2020-11-27 00:22:05 +0900, Jun. T:
> 
>> Zsh only need to check whether the new limit user wants to set is
>> within the range of rlim_t.
> 
> Yes, but how do you determine that range? Should we not also
> reject 18446744073709551615 as out-of-range on systems where
> it's RLIM_INFINITY since it's not preventing file sizes to get
> past 18446744073709551615 for instance.

I guess you mean we should reject RLIM_INFINITY, and yes I agree with it.
How about the patch below (to your v2)?

# Even if you accept this patch, maybe better to commit it separately after
# your patch (which may be separated into parts) for finer granularity.

Jun


diff --git a/Src/Builtins/rlimits.c b/Src/Builtins/rlimits.c
index b6568d956..69174fe8e 100644
--- a/Src/Builtins/rlimits.c
+++ b/Src/Builtins/rlimits.c
@@ -297,7 +297,7 @@ printrlim(rlim_t val, const char *unit)
 static rlim_t
 zstrtorlimt(const char *s, int lim, int ulimit, char **err)
 {
-    rlim_t ret = 0;
+    rlim_t ret = 0, tmp;
     const char *orig = s;
     enum zlimtype type = resinfo[lim]->type;
     *err = NULL;
@@ -305,8 +305,14 @@ zstrtorlimt(const char *s, int lim, int ulimit, char **err)
     if (strcmp(s, "unlimited") == 0)
 	return RLIM_INFINITY;
 
-    for (; *s >= '0' && *s <= '9'; s++)
-	ret = ret * 10 + *s - '0';
+    for (; *s >= '0' && *s <= '9'; s++) {
+	if ((tmp = ret * 10 + *s - '0') < ret) {
+	    *err = "limit out of range";
+	    return 0;
+	}
+	else
+	    ret = tmp;
+    }
 
     if (s == orig) {
 	*err = "decimal integer expected";
@@ -412,6 +418,7 @@ zstrtorlimt(const char *s, int lim, int ulimit, char **err)
 	/*
 	 * memory-type resource
 	 */
+	rlim_t unit = 1;
 	if (*s) {
 	    if (*s == 'b' || *s == 'B')
 		s++;
@@ -425,7 +432,7 @@ zstrtorlimt(const char *s, int lim, int ulimit, char **err)
 			/* KB == 1000 */
 			const char *p;
 			for (p = suffix; p <= offset; p += 2)
-			    ret *= 1000;
+			    unit *= 1000;
 			s++;
 		    }
 		    else {
@@ -433,7 +440,7 @@ zstrtorlimt(const char *s, int lim, int ulimit, char **err)
 			if ((s[0] == 'i' || s[0] == 'I') &&
 			    (s[1] == 'b' || s[1] == 'B'))
 			    s += 2;
-			ret <<= ((offset - suffix) / 2 + 1) * 10;
+			unit <<= ((offset - suffix) / 2 + 1) * 10;
 		    }
 		}
 	    }
@@ -444,7 +451,7 @@ zstrtorlimt(const char *s, int lim, int ulimit, char **err)
 	}
 	else {
 	    if (ulimit)
-		ret *= resinfo[lim]->unit;
+		unit = resinfo[lim]->unit;
 	    else
 #ifdef HAVE_RLIMIT_MSGQUEUE
 		if (lim != RLIMIT_MSGQUEUE)
@@ -462,8 +469,19 @@ zstrtorlimt(const char *s, int lim, int ulimit, char **err)
 		     * compatibility with tcsh.
 		     */
 #endif
-		    ret *= 1024;
+		    unit = 1024;
 	}
+	if ((tmp = ret*unit) < ret) {
+	    *err = "limit out of range";
+	    return 0;
+	}
+	else
+	    ret = tmp;
+    }
+    if (ret == RLIM_INFINITY) {
+	/* RLIM_INFINITY can be specified only by the string "unlimited" */
+	*err = "limit out of range";
+	return 0;
     }
     return ret;
 }






  reply	other threads:[~2020-11-27 18:24 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-23 21:49 Stephane Chazelas
2020-11-25  0:35 ` Daniel Shahaf
2020-11-25  6:44   ` Stephane Chazelas
2020-11-27 17:16     ` Daniel Shahaf
2020-11-26  6:57   ` [PATCHv2 1/2] [long] improvements to limit/ulimit API and doc ((un)limit in csh emulation) Stephane Chazelas
2020-11-25 23:43 ` [PATCHv1] [long] improvements to limit/ulimit API and doc Oliver Kiddle
2020-11-26 20:14   ` [PATCH] ulimit option completions using ulimit -a output Stephane Chazelas
2020-11-27  7:13     ` Stephane Chazelas
2020-11-27  8:15       ` Felipe Contreras
2020-11-27 12:19       ` Oliver Kiddle
2021-03-27 21:25         ` Lawrence Velázquez
2021-04-03 14:57           ` Lawrence Velázquez
2021-04-10 20:11             ` Lawrence Velázquez
2021-04-13 14:35     ` Daniel Shahaf
2021-05-09 20:37       ` Lawrence Velázquez
2021-05-11 19:05         ` Stephane Chazelas
2020-11-26 20:58   ` [PATCHv2 2/2] [long] improvements to limit/ulimit API and doc (the rest) Stephane Chazelas
2020-11-27 16:39     ` Daniel Shahaf
2020-11-27 20:13       ` Stephane Chazelas
2020-11-27 20:36         ` Daniel Shahaf
2020-11-28  6:52           ` zsh coding style (was about a limit patch review) Stephane Chazelas
2020-12-01 16:47             ` Daniel Shahaf
2020-11-28  8:16         ` [PATCHv3 2/2] [long] improvements to limit/ulimit API and doc (the rest) Stephane Chazelas
2021-03-27 21:21           ` Lawrence Velázquez
2021-03-31 18:06             ` Stephane Chazelas
2020-11-26 11:19 ` [PATCHv1] [long] improvements to limit/ulimit API and doc Jun T
2020-11-26 13:55   ` Stephane Chazelas
2020-11-26 15:22     ` Jun. T
2020-11-26 17:23       ` Stephane Chazelas
2020-11-27 18:24         ` Jun. T [this message]
2020-11-27 18:34           ` Daniel Shahaf
2020-11-27 20:46           ` Stephane Chazelas

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=FF7E5A4B-E00F-4160-AFBD-45783C0BAF7B@kba.biglobe.ne.jp \
    --to=takimoto-j@kba.biglobe.ne.jp \
    --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).