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 8581955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:10:28+00:00 2026-06-11T21:10:28+00:00

I know strcpy is not secure when dealing with buffers, however in my code

  • 0

I know strcpy is not secure when dealing with buffers, however in my code i have this

static char    buf[HUGE_BUFFER_SIZE + 1];
char servername[200];
int length = 0;
char *out = NULL;
        length = strlen(buf);
        out = alloca(strlen(servername) + length + 5);
        length = strlen(servername);
        strcpy(out, servername);
        out[length] = ' ';
        out[length + 1] = 0;
        strcat(out, buf);

If I change strcpy to strlcpy and use a sizeof for arg3, the output is garbled. Is there an easier way than using memcpy (which looks like I might have to do?) Im trying to avoid using insecure functions like strcpy and friends. Is there an easier way to accomplish the above with less copying of functions, and strnlens/alloca?

  • 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-11T21:10:29+00:00Added an answer on June 11, 2026 at 9:10 pm

    I’d do it like this:

    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    #define HUGE_BUFFER_SIZE 4096
    
    int main (int argc, char **argv) {
      static char buf[HUGE_BUFFER_SIZE + 1];
      char servername[200];
      int target_len, servername_len, buf_len = 0;
      char *out = NULL;
    
      strcpy(buf, "this is my test buffer!");
      strcpy(servername, "127.0.0.1");
    
      /* Notice the uses of strnlen.  Each of these fixes the maximum number of characters that will be scanned. */
      servername_len = strnlen(servername, HUGE_BUFFER_SIZE+1) + 1;
      buf_len = strnlen(buf, 200) + 4;
      target_len = servername_len, buf_len;
    
      if ((out = alloca(target_len)) == NULL)
        return EXIT_FAILURE; /* alloc failed, die quickly! */
    
      /* At this point, you've measured the length of servername and buf using strlen, so it should really be impossible to run beyond the length of your buffer, but we're going to be careful, anyway */
      out[0] = '\0';
      strncat(out, servername, servername_len);
      strncat(out, buf, buf_len);
    
      printf("%s\n", out);
    
      return EXIT_SUCCESS;
    }
    

    (barring a copy/paste error, or different libraries on your system and a cross-platform screwup on my part, you should be able to paste this into a file, compile it, and run it, and see “127.0.0.1this is my test buffer!” as the output)

    Note that all of the str functions, whether strcpy, strcat, strncpy, or strlcpy, drop a null terminator onto the end of any string they create. The only time you need to manually put on a null terminator is when you are starting a brand new string, as I did with the statement “out[0] = ‘\0′”.

    Also, note that I used strnlen and strncat, not strlcat. I cannot tell from the documentation the difference between strlcat and strncat except in what the function returns.

    Anyways, you should be able to avoid buffer-overrun security holes just be clearly bounding all of your function calls. strncpy and strncat bound how much data you will copy. strnlen bounds how long the system will search for a ‘\0’.


    Edit: my original solution actually had an error that valgrind revealed. I was not initializing either buf or servername. While something else seemed to be magically initializing buf, valgrind reported that calling strcat with servername as the target was causing decisions to be made based on uninitialized data. doh.

    Since I’m not sure why I was calling strcat to initialize my variables with string literals, I switched to strcpy. That, of course, is perfectly safe here because I have hard-coded into the application exactly the string I’m copying into the variable.

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

Sidebar

Related Questions

I'm new with Objective C. I would like to know why this code does
I need help with debugging this piece of code. I know the problem is
Why the following code works: char *p; p=hello; printf(%s\n,p); While this one doesn't: char
I was curious about why this is not allowed in C: char myarray[4]; myarray
Why this code is not running? Why str1 is not assigned to str2 ??
Know this might be rather basic, but I been trying to figure out how
I know you can not set a key value dynamically, but what about the
I know there have been many questions on grid and pack in the past
I know that this sort of question has been asked here before, but still
i know this is a stupid question but i d'ont know how to do

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.