reduce scope of variable. Found by PVS-Studio
[freeradius.git] / src / main / exec.c
index 48ff6ea..d5b3a9a 100644 (file)
@@ -86,22 +86,29 @@ pid_t radius_start_program(char const *cmd, REQUEST *request, bool exec_wait,
                           VALUE_PAIR *input_pairs, bool shell_escape)
 {
 #ifndef __MINGW32__
-       char *p;
        VALUE_PAIR *vp;
        int n;
        int to_child[2] = {-1, -1};
        int from_child[2] = {-1, -1};
        pid_t pid;
 #endif
-       int argc;
-       int i;
-       char *argv[MAX_ARGV];
-       char argv_buf[4096];
+       int             argc;
+       int             i;
+       char const      **argv_p;
+       char            *argv[MAX_ARGV], **argv_start = argv;
+       char            argv_buf[4096];
 #define MAX_ENVP 1024
        char *envp[MAX_ENVP];
        int envlen = 0;
 
-       argc = rad_expand_xlat(request, cmd, MAX_ARGV, argv, true, sizeof(argv_buf), argv_buf);
+       /*
+        *      Stupid array decomposition...
+        *
+        *      If we do memcpy(&argv_p, &argv, sizeof(argv_p)) src ends up being a char **
+        *      pointing to the value of the first element.
+        */
+       memcpy(&argv_p, &argv_start, sizeof(argv_p));
+       argc = rad_expand_xlat(request, cmd, MAX_ARGV, argv_p, true, sizeof(argv_buf), argv_buf);
        if (argc <= 0) {
                DEBUG("invalid command line '%s'.", cmd);
                return -1;
@@ -109,7 +116,7 @@ pid_t radius_start_program(char const *cmd, REQUEST *request, bool exec_wait,
 
 
 #ifndef NDEBUG
-       if (debug_flag > 2) {
+       if (rad_debug_lvl > 2) {
                DEBUG3("executing cmd %s", cmd);
                for (i = 0; i < argc; i++) {
                        DEBUG3("\t[%d] %s", i, argv[i]);
@@ -161,6 +168,8 @@ pid_t radius_start_program(char const *cmd, REQUEST *request, bool exec_wait,
                         */
                        snprintf(buffer, sizeof(buffer), "%s=", vp->da->name);
                        if (shell_escape) {
+                               char *p;
+
                                for (p = buffer; *p != '='; p++) {
                                        if (*p == '-') {
                                                *p = '_';
@@ -251,7 +260,7 @@ pid_t radius_start_program(char const *cmd, REQUEST *request, bool exec_wait,
                 *      If we are debugging, then we want the error
                 *      messages to go to the STDERR of the server.
                 */
-               if (debug_flag == 0) {
+               if (rad_debug_lvl == 0) {
                        dup2(devnull, STDERR_FILENO);
                }
                close(devnull);
@@ -428,7 +437,7 @@ int radius_readfrom_program(int fd, pid_t pid, int timeout,
                rcode = select(fd + 1, &fds, NULL, NULL, &wake);
                if (rcode == 0) {
                too_long:
-                       DEBUG("Child PID %u is taking too much time: forcing failure and killing child.", pid);
+                       DEBUG("Child PID %u is taking too much time: forcing failure and killing child.", (unsigned int) pid);
                        kill(pid, SIGTERM);
                        close(fd); /* should give SIGPIPE to child, too */
 
@@ -499,6 +508,7 @@ int radius_readfrom_program(int fd, pid_t pid, int timeout,
 
 /** Execute a program.
  *
+ * @param[in,out] ctx to allocate new VALUE_PAIR (s) in.
  * @param[out] out buffer to append plaintext (non valuepair) output.
  * @param[in] outlen length of out buffer.
  * @param[out] output_pairs list of value pairs - child stdout will be parsed and added into this list
@@ -513,7 +523,7 @@ int radius_readfrom_program(int fd, pid_t pid, int timeout,
 
  * @return 0 if exec_wait==0, exit code if exec_wait!=0, -1 on error.
  */
-int radius_exec_program(char *out, size_t outlen, VALUE_PAIR **output_pairs,
+int radius_exec_program(TALLOC_CTX *ctx, char *out, size_t outlen, VALUE_PAIR **output_pairs,
                        REQUEST *request, char const *cmd, VALUE_PAIR *input_pairs,
                        bool exec_wait, bool shell_escape, int timeout)
 
@@ -571,9 +581,9 @@ int radius_exec_program(char *out, size_t outlen, VALUE_PAIR **output_pairs,
        if (output_pairs) {
                /*
                 *      HACK: Replace '\n' with ',' so that
-                *      userparse() can parse the buffer in
+                *      fr_pair_list_afrom_str() can parse the buffer in
                 *      one go (the proper way would be to
-                *      fix userparse(), but oh well).
+                *      fix fr_pair_list_afrom_str(), but oh well).
                 */
                for (p = answer; *p; p++) {
                        if (*p == '\n') {
@@ -593,7 +603,7 @@ int radius_exec_program(char *out, size_t outlen, VALUE_PAIR **output_pairs,
                        answer[--len] = '\0';
                }
 
-               if (userparse(request, answer, output_pairs) == T_INVALID) {
+               if (fr_pair_list_afrom_str(ctx, answer, output_pairs) == T_INVALID) {
                        RERROR("Failed parsing output from: %s: %s", cmd, fr_strerror());
                        strlcpy(out, answer, len);
                        ret = -1;