Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: [PATCHv1] [long] improvements to limit/ulimit API and doc



> 2020/11/27 2:23, Stephane Chazelas <stephane@xxxxxxxxxxxx> 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;
 }







Messages sorted by: Reverse Date, Date, Thread, Author