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

  • SEARCH
  • Home
  • 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 9095441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:36:05+00:00 2026-06-16T23:36:05+00:00

#define BUFSIZE 256 int escape_single_quotes(char *to, char *from, int max) { int l= 0;

  • 0
#define BUFSIZE 256

int escape_single_quotes(char *to, char *from, int max)
{  int l= 0;

  for (;*from;from++)
  {
    switch (*from)    {
      case '\'':
        if (l>=max-4) return 0; // not enough space for escaped chars!
// replace ' with '\''
        *(to++)= '\'';       
        *(to++)= '\\';
        *(to++)= '\'';
        l += 3;
      default:
        if (l>=max-1) return 0; // not enough space for this char!       
        *(to++)= *from;
        l++;
    }
  }
   *to= 0;

  return 1;
}
 int main(int argc, char *argv[])
{
  FILE *fp;
  char buf[BUFSIZE];
  char escaped_name[BUFSIZE];  char *filename;
  int lines, chars, cpl;


   if (argc != 2)
  {
    fprintf(stderr, "Usage: %s filename\n", argv[0]);
    exit(EXIT_FAILURE);
  }  
  filename= argv[1];
  printf("Counting %s ... \n", filename);

  if (!escape_single_quotes(escaped_name, filename, BUFSIZE))  {
    fprintf(stderr, "Escaped filename is too long!\n");
    exit(EXIT_FAILURE);
  }
   if (snprintf(buf, BUFSIZE, "wc -l '%s'", escaped_name)>=BUFSIZE)
  {
    fprintf(stderr, "Filename %s is too long!\n", filename);
    exit(EXIT_FAILURE);
  } 
  fp= popen(buf, "r");}

Is this script safe from command injection? It replaces ‘ by ‘\”. Is there anyway to break from single quotes and inject a command?

  • 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-16T23:36:07+00:00Added an answer on June 16, 2026 at 11:36 pm

    If I read this program correctly, it simply calls wc -l on its filename parameter.

    When you are trying to escape this filename, it probably means it comes from source that you cannot trust.

    Given that, few points:

    • Rather than that painfully escaping filename, you can simply check if such filename exists using access() call, and if it does, you can pass it to wc without much concern.
    • I would not trust even whoever calls this very program, which means you cannot trust your own PATH. In other words, when you call wc -l, you may very will be calling something evil, because somebody did something along these lines:

      cp evilwc /tmp/wc
      export PATH=/tmp:$PATH
      ./yourprogram goodfilename
      

    Oops! Even if file name was good, attacker still was able to trick your program to calling evil code.

    Solutions:

    In this particular case, you can probably implement wc -l on your own and not call anything external – it is very simple algorithm after all.

    Also, when calling external programs, you should try to call them by full name, like /usr/bin/wc.

    If parameters are filenames, it is good idea to check if they actually exist before passing them over.

    You may want to sanitize your parameters for valid chars, but this is easy to do wrong, and for example remove non-ASCII symbols that are crucial for all non-English speaking world.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey guys, here is my code. int main() { char buffer[BUFSIZE]; // define our
I have this struct; #define BUFSIZE 10 struct shared_data { pthread_mutex_t th_mutex_queue; int count;
#define MAX 100 struct bs{ int ab; int ac; }be; struct s{ be b;
#define HISTORY_SIZE 50 #define INPUT_SIZE 512 /*Max input size*/ char input[INPUT_SIZE]; /*Holding user input
I am using this code to get the windows version: #define BUFSIZE 256 bool
In kernel space, I have the following: char * myData[MAX_BUF_SIZE][2]; I need to define
int main() { int a = 0; int BUFSIZE = 1000; char *string1[20]; FILE
#define BUF_SIZE 10 char *html = foo:baa\r\nxxx:yyyy:\r\nLocation:........................................\r\Connection:close\r\n\r\n; char *p = (char*)html, *buf, *pbuf, *tbuf;
#define __HAVE_ARCH_STRCPY What's the meaning of __HAVE_ARCH ? I'm not a native speaker and
#define f(g,g2) g##g2 main() { int var12=100; printf(%d,f(var,12)); } The above program prints 100

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.