[PATCH] Core: parse octal/hexadecimal numeric config directives

Ari Aosved ari.aosved at gmail.com
Fri Nov 7 21:19:43 UTC 2014


# HG changeset patch
# User Ari Aosved <ari.aosved at gmail.com>
# Date 1415392382 28800
#      Fri Nov 07 12:33:02 2014 -0800
# Node ID 742fb1a59b705ba71f2bab5fede58ff5885ebc71
# Parent  234c5ecb00c04c67bcbb4a3be45fd844f2289462
Core: parse octal/hexadecimal numeric config directives.

When working with modules which need configuration directives dealing with
file modes, it's convenient to specify the numeric values in octal notation.
For instance "hls_directory_create_mode 0775;" is recognizably rwxrwxr-x
whereas "hls_directory_create_mode 509;" would be more prone to mistakes.

diff -r 234c5ecb00c0 -r 742fb1a59b70 src/core/ngx_conf_file.c
--- a/src/core/ngx_conf_file.c	Fri Nov 07 17:38:55 2014 +0300
+++ b/src/core/ngx_conf_file.c	Fri Nov 07 12:33:02 2014 -0800
@@ -1097,7 +1097,16 @@
     }
 
     value = cf->args->elts;
-    *np = ngx_atoi(value[1].data, value[1].len);
+    if (value[1].len > 2 && *value[1].data == '0' && *value[1].data+1 == 'x') {
+        *np = ngx_hextoi(value[1].data+2, value[1].len - 2);
+    }
+    else
+    if (value[1].len > 1 && *value[1].data == '0') {
+        *np = ngx_octtoi(value[1].data+1, value[1].len - 1);
+    }
+    else {
+        *np = ngx_atoi(value[1].data, value[1].len);
+    }
     if (*np == NGX_ERROR) {
         return "invalid number";
     }
diff -r 234c5ecb00c0 -r 742fb1a59b70 src/core/ngx_string.c
--- a/src/core/ngx_string.c	Fri Nov 07 17:38:55 2014 +0300
+++ b/src/core/ngx_string.c	Fri Nov 07 12:33:02 2014 -0800
@@ -1085,6 +1085,36 @@
 }
 
 
+ngx_int_t
+ngx_octtoi(u_char *line, size_t n)
+{
+    u_char     ch;
+    ngx_int_t  value;
+
+    if (n == 0) {
+        return NGX_ERROR;
+    }
+
+    for (value = 0; n--; line++) {
+        ch = *line;
+
+        if (ch >= '0' && ch <= '7') {
+            value = value * 8 + (ch - '0');
+            continue;
+        }
+
+        return NGX_ERROR;
+    }
+
+    if (value < 0) {
+        return NGX_ERROR;
+
+    } else {
+        return value;
+    }
+}
+
+
 u_char *
 ngx_hex_dump(u_char *dst, u_char *src, size_t len)
 {
diff -r 234c5ecb00c0 -r 742fb1a59b70 src/core/ngx_string.h
--- a/src/core/ngx_string.h	Fri Nov 07 17:38:55 2014 +0300
+++ b/src/core/ngx_string.h	Fri Nov 07 12:33:02 2014 -0800
@@ -175,6 +175,7 @@
 off_t ngx_atoof(u_char *line, size_t n);
 time_t ngx_atotm(u_char *line, size_t n);
 ngx_int_t ngx_hextoi(u_char *line, size_t n);
+ngx_int_t ngx_octtoi(u_char *line, size_t n);
 
 u_char *ngx_hex_dump(u_char *dst, u_char *src, size_t len);
 



More information about the nginx-devel mailing list