spacewander commented on code in PR #8390:
URL: https://github.com/apache/apisix/pull/8390#discussion_r1031944377


##########
apisix/core/env.lua:
##########
@@ -0,0 +1,107 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local ffi         = require "ffi"
+
+local json        = require("apisix.core.json")
+local log         = require("apisix.core.log")
+local string      = require("apisix.core.string")
+
+local os          = os
+local type        = type
+local upper       = string.upper
+local find        = string.find
+local sub         = string.sub
+local str         = ffi.string
+local C           = ffi.C
+
+local _M = {}
+
+local ENV_PREFIX = "$ENV://"
+
+local apisix_env_vars = {}
+
+ffi.cdef [[
+  extern char **environ;
+  int memcmp(const void *s1, const void *s2, size_t n);
+]]
+
+
+function _M.init()
+  local e = ffi.C.environ
+  if not e then
+    log.warn("could not access environment variables")
+    return
+  end
+
+  local i = 0
+  while e[i] ~= nil do
+    local var = str(e[i])
+    local p = find(var, "=")
+    if p then
+        apisix_env_vars[sub(var, 1, p - 1)] = sub(var, p + 1)
+    end
+
+    i = i + 1
+  end
+end
+
+
+local function is_env_ref(ref)
+    -- We will not use string.has_prefix,
+    -- to avoid the error caused by has_prefix to cause a crash.
+    return type(ref) == "string" and #ref > 7 and
+        0 == C.memcmp(upper(ref), ENV_PREFIX, 7)
+end
+
+
+local function parse_ref(ref)
+    local path = sub(ref, 8)

Review Comment:
   ```suggestion
       local path = sub(ref, #ENV_PREFIX + 1)
   ```
   can remove the magic number.



##########
apisix/core/env.lua:
##########
@@ -0,0 +1,107 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local ffi         = require "ffi"
+
+local json        = require("apisix.core.json")
+local log         = require("apisix.core.log")
+local string      = require("apisix.core.string")
+
+local os          = os
+local type        = type
+local upper       = string.upper
+local find        = string.find
+local sub         = string.sub
+local str         = ffi.string
+local C           = ffi.C
+
+local _M = {}
+
+local ENV_PREFIX = "$ENV://"
+
+local apisix_env_vars = {}
+
+ffi.cdef [[
+  extern char **environ;
+  int memcmp(const void *s1, const void *s2, size_t n);
+]]
+
+
+function _M.init()
+  local e = ffi.C.environ

Review Comment:
   ```suggestion
     local e = C.environ
   ```



##########
apisix/core/env.lua:
##########
@@ -0,0 +1,107 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local ffi         = require "ffi"
+
+local json        = require("apisix.core.json")
+local log         = require("apisix.core.log")
+local string      = require("apisix.core.string")
+
+local os          = os
+local type        = type
+local upper       = string.upper
+local find        = string.find
+local sub         = string.sub
+local str         = ffi.string
+local C           = ffi.C
+
+local _M = {}
+
+local ENV_PREFIX = "$ENV://"
+
+local apisix_env_vars = {}
+
+ffi.cdef [[
+  extern char **environ;
+  int memcmp(const void *s1, const void *s2, size_t n);
+]]
+
+
+function _M.init()
+  local e = ffi.C.environ
+  if not e then
+    log.warn("could not access environment variables")
+    return
+  end
+
+  local i = 0
+  while e[i] ~= nil do
+    local var = str(e[i])
+    local p = find(var, "=")
+    if p then
+        apisix_env_vars[sub(var, 1, p - 1)] = sub(var, p + 1)
+    end
+
+    i = i + 1
+  end
+end
+
+
+local function is_env_ref(ref)
+    -- We will not use string.has_prefix,
+    -- to avoid the error caused by has_prefix to cause a crash.
+    return type(ref) == "string" and #ref > 7 and
+        0 == C.memcmp(upper(ref), ENV_PREFIX, 7)

Review Comment:
   `type(ref) == "string" and string.has_prefix(upper(ref), ENV_PREFIX)`, will 
this way cause a crash?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to