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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:42:04+00:00 2026-05-15T03:42:04+00:00

gcc 4.4.4 c89 My program does a lot of string coping. I don’t want

  • 0

gcc 4.4.4 c89

My program does a lot of string coping. I don’t want to use the strncpy as it doesn’t nul terminate. And I can’t use strlcpy as its not portable.

Just a few questions. How can I put my function through its paces to ensure that it is completely safe and stable. Unit testing?

Is this good enough for production?

size_t s_strlcpy(char *dest, const char *src, const size_t len)
{
    size_t i = 0;

    /* Always copy 1 less then the destination to make room for the nul */
    for(i = 0; i < len - 1; i++)
    {
        /* only copy up to the first nul is reached */
        if(*src != '\0') {
            *dest++ = *src++;
        }
        else {
            break;
        }
    }

    /* nul terminate the string */
    *dest = '\0';

    /* Return the number of bytes copied */
    return i;
}

Many thanks for any suggestions,

  • 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-05-15T03:42:05+00:00Added an answer on May 15, 2026 at 3:42 am

    Although you could simply use another strlcpy function as another post recommends, or use snprintf(dest, len, "%s", src) (which always terminates the buffer), here are the things I noticed looking at your code:

    size_t s_strlcpy(char *dest, const char *src, const size_t len)
    {
        size_t i = 0;
    

    No need to make len const here, but it can be helpful since it checks to make sure you didn’t modify it.

        /* Always copy 1 less then the destination to make room for the nul */
        for(i = 0; i < len - 1; i++)
        {
    

    Oops. What if len is 0? size_t is usually unsigned, so (size_t)0 – 1 will end up becoming something like 4294967295, causing your routine to careen through your program’s memory and crash into an unmapped page.

            /* only copy up to the first nul is reached */
            if(*src != '\0') {
                *dest++ = *src++;
            }
            else {
                break;
            }
        }
    
        /* nul terminate the string */
        *dest = '\0';
    

    The above code looks fine to me.

        /* Return the number of bytes copied */
        return i;
    }
    

    According to Wikipedia, strlcpy returns strlen(src) (the actual length of the string), not the number of bytes copied. Hence, you need to keep counting the characters in src until you hit '\0', even if it exceeds len.

    Also, if your for loop terminates on the len - 1 condition, your function will return len-1, not len like you’d expect it to.


    When I write functions like this, I usually prefer to use a start pointer (call it S) and end pointer (call it E). S points to the first character, while E points to one character after the last character (which makes it so E – S is the length of the string). Although this technique may seem ugly and obscure, I’ve found it to be fairly robust.

    Here’s an over-commented version of how I would write strlcpy:

    size_t s_strlcpy(char *dest, const char *src, size_t len)
    {
        char *d = dest;
        char *e = dest + len; /* end of destination buffer */
        const char *s = src;
    
        /* Insert characters into the destination buffer
           until we reach the end of the source string
           or the end of the destination buffer, whichever
           comes first. */
        while (*s != '\0' && d < e)
            *d++ = *s++;
    
        /* Terminate the destination buffer, being wary of the fact
           that len might be zero. */
        if (d < e)        // If the destination buffer still has room.
            *d = 0;
        else if (len > 0) // We ran out of room, so zero out the last char
                          // (if the destination buffer has any items at all).
            d[-1] = 0;
    
        /* Advance to the end of the source string. */
        while (*s != '\0')
            s++;
    
        /* Return the number of characters
           between *src and *s,
           including *src but not including *s . 
           This is the length of the source string. */
        return s - src;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

gcc (GCC) 4.6.3 c89 I am trying to use usleep . However, I keep
gcc 4.4.4 c89 I have a program that I am testing. I create a
gcc 4.6.2 c89 I have the following 2D array that I want to pass
gcc 4.4.2 c89 I was just working on some pointers. However, with the program
gcc 4.4.2 c89 I have a file called main.c. I want the result of
gcc 4.6.1 c89 I have a string that I need parse. The string is
gcc 4.4.3 c89 pthreads I use valgrind for checking memory errors. I am just
gcc c89 I am just looking to link against the below shared library. I
gcc 4.4.2 c89 I have a wave file: 8000 Hz 16 bit I am
gcc 4.4.2 c89 I have a function that has to run (config_relays). It make

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.