title: dealing with the occurrence of buffer overflow and indefinite behavior in a program that performs a two-way communication
body: <p>how can I modify the program so as to eliminate the danger of buffer overflow and modify it so as to prevent undefined behavior in the sequence <code>if (isalpha (res_buf [i])) res_buf [i] = toupper (res_buf [ i]);</code>?</p>
  <p>The implementation uses 2 pipes for a two-way communication. (parent-child)</p>
  <p>I would also like to say a few works about the performance of the program</p>
  <pre><code>#include &lt;unistd.h&gt;
  #include &lt;sys/wait.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;ctype.h&gt;
  
  #define MAXBUF 1024
  
  void err_exit(const char *msg)
  {
      printf(&quot;%s\n&quot;, msg);
      exit(EXIT_SUCCESS);
  }
  
  int main()
  {
      char buf[MAXBUF];
      int parent_w[2], child_w[2];
      ssize_t n;
      char res_buf[MAXBUF];
      size_t sz;
  
      if (pipe(parent_w) == -1)
          err_exit(&quot;pipe&quot;);
      if (pipe(child_w) == -1)
          err_exit(&quot;pipe&quot;);
  
      pid_t pid = fork();
      switch (pid)
      {
      case -1:
          err_exit(&quot;fork&quot;);
  
      case 0:
          /* child */
          if (close(parent_w[1]) == -1)
              err_exit(&quot;close&quot;);
          if (close(child_w[0]) == -1)
              err_exit(&quot;close&quot;);
  
          while (1)
          {
              strcpy(res_buf, &quot;&quot;);
              n = read(parent_w[0], buf, MAXBUF);
              if (n == -1)
                  err_exit(&quot;read&quot;);
  
              if (n == 0)
                  break;
  
              strncpy(res_buf, buf, MAXBUF);
              sz = strlen(res_buf);
              for (int i = 0; i &lt; sz; ++i)
                  if (isalpha(res_buf[i]))
                      res_buf[i] = toupper(res_buf[i]);
  
              if (write(child_w[1], res_buf, strlen(res_buf) + 1) != strlen(buf) + 1)
                  err_exit(&quot;write&quot;);
          }
  
          if (close(parent_w[0]) == -1)
              err_exit(&quot;close&quot;);
          if (close(child_w[1]) == -1)
              err_exit(&quot;close&quot;);
  
          _exit(EXIT_SUCCESS);
      }
  
      /* parent */
      if (close(parent_w[0]) == -1)
          err_exit(&quot;close&quot;);
      if (close(child_w[1]) == -1)
          err_exit(&quot;close&quot;);
  
      while (1)
      {
          if (scanf(&quot;%s&quot;, buf) == -1)
              break;
  
          if (write(parent_w[1], buf, strlen(buf) + 1) != strlen(buf) + 1)
              err_exit(&quot;write&quot;);
  
          strcpy(res_buf, &quot;&quot;);
          n = read(child_w[0], buf, MAXBUF);
          if (n == -1)
              err_exit(&quot;read&quot;);
  
          printf(&quot;%s\n&quot;, buf);
      }
  
      if (close(parent_w[1]) == -1)
          err_exit(&quot;close&quot;);
      if (close(child_w[0]) == -1)
          err_exit(&quot;close&quot;);
  
      waitpid(pid, NULL, 0);
  
      return 0;
  }
  </code></pre>