Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8085543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:09:20+00:00 2026-06-05T18:09:20+00:00

I use a simple pipe. I read with a while, 1 char at a

  • 0

I use a simple pipe. I read with a while, 1 char at a time, I think every time I read a char I overwrite something

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <string.h>

int main () {
    int pipefd[2];
    int cpid;
    char buf[31];
    if (pipe(pipefd) == -1) {
        perror("pipe");
        exit(EXIT_FAILURE);
    }
    cpid = fork();
    if (cpid == -1) {
        perror("cpid");
        exit(EXIT_FAILURE);
    }
    if (cpid == 0) {      // child reads from pipe
        close (pipefd[1]); // close unused write end
        int i = 0;
        while (read (pipefd[0], &(buf[i++]), 1)>0);
        printf ("Server receives: %s", buf);
        close (pipefd[0]);
        exit (EXIT_SUCCESS);
    }
    else {               // parent writes to pipe
        close (pipefd[0]); // closing unused read end;
        char buf2[30];
        printf("Server transmits: ");
        scanf ("%s", buf2);
        write (pipefd[1], buf2, strlen(buf2)+1);
        close(pipefd[1]);
        wait(NULL);
        exit(EXIT_SUCCESS);
    }
  return 0;
}

Code is now fixed, this is obsolete: For example, if I input: “Flowers” it prints F and then ~6 unprintable characters

BUT: A small odd thing that’s happening, I just used a string way longer than 30, and it produced no error at all, and it did manage to write the entire string. Though both my buffers are considerably smaller than that.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T18:09:22+00:00Added an answer on June 5, 2026 at 6:09 pm

    (Following Anders’ suggestion.)

    Using GCC 4.6.3, my code:

    #include <unistd.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/wait.h>
    #include <string.h>
    
    int main () {
        int pipefd[2];
        int cpid;
        char buf[31];
        if (pipe(pipefd) == -1) {
            perror("pipe");
            exit(EXIT_FAILURE);
        }
        cpid = fork();
        if (cpid == -1)
        {
            perror("cpid");
            exit(EXIT_FAILURE);
        }
        if (cpid == 0) {      // child reads from pipe
            close (pipefd[1]); // close unused write end
            int i=0;
            while (read(pipefd[0], &(buf[i++]), 1) != 0);
            printf ("Server receives: %s\n", buf);
            close (pipefd[0]);
            exit (EXIT_SUCCESS);
        }
        else {               // parent writes to pipe
            close (pipefd[0]); // closing unused read end;
            char buf2[30];
            printf("Server transmits: ");
            scanf ("%s", buf2);
            write (pipefd[1], buf2, strlen(buf2)+1);
            close(pipefd[1]);
            wait(NULL);
            exit(EXIT_SUCCESS);
        }
      return 0;
    }
    

    Produces:

    [user@host tmp]$ gcc pipes.c -o pipes && ./pipes 
    Server transmits: Flowers
    Server receives: Flowers
    

    (I also agree with his sentiment about bounds checking.)


    EDIT: Per your comment, if you change the following line (35 for me)

    scanf("%s", buf2);
    

    to

    fgets(buf2, 30, stdin);
    

    You gain two benefits. (a) You eliminate a buffer overflow vulnerability by limiting the number of bytes that will be copied into buf2. (b) You are able to “accept” non-newline whitespace (spaces and tabs) whereas with scanf, you were not:

    [user@host tmp]$ gcc pipes.c -o pipes && ./pipes 
    Server transmits: Flowers smell nice
    Server receives: Flowers smell nice
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is simple demonstration of pipe fork exec trio using in unix. #include <stdio.h>
How to use simple TCP/IP sockets connections over/with Net.TCP WCF binding? Idea is –
I'm trying to use Simple Savant within my application, to use SimpleDB I currently
We often use simple enumerations to represent a state on our entities. The problem
Is there a way to use simple sql queries on ASP MVC without using
I've always thought it was great that I could use simple iconic unicode characters
use LWP::Simple; use Parallel::ForkManager; @links=( [http://prdownloads.sourceforge.net/sweethome3d/SweetHome3D-2.1-windows.exe,SweetHome3D-2.1-windows.exe], [http://prdownloads.sourceforge.net/sweethome3d/SweetHome3D-2.1-macosx.dmg,SweetHome3D-2.1-macosx.dmg], [http://prdownloads.sourceforge.net/sweethome3d/SweetHome3DViewer-2.1.zip,SweetHome3DViewer-2.1.zip], ); # Max 30 processes for
I want to use the Simple Query tool in SubSonic 3(.0.0.2) and the docs
Scenario: I use a simple function to minify and compress JS files during the
TCP and UDP(sometimes) use a simple checksum to make sure the contents are correct.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.