strcat Vs strncat - When should which function be used?

后端 未结 5 851
无人及你
无人及你 2021-01-02 11:55

Some static code analyzer tools are suggesting that all strcat usage should be replaced with strncat for safety purpose?

In a program, if we know clearly the size of

相关标签:
5条回答
  • 2021-01-02 12:11

    It's very simple strcat is used to concatenate two strings , for example

    String a= data 
    String b = structures 
    

    If use perform strcat

    Strcat(a, b) 
    

    then

    a= data structures
    

    But if you want to concatenate specific numer of word r elements then you can use strncat Example if you want to concatenate only the first two alphabet lts of b into a then you have to write Strncat(a,b,2) (It means that you just cancatenate the fist two alphabets of b into a , and a becomes a = data st

    0 讨论(0)
  • 2021-01-02 12:19

    They don't do the same thing so they can't be substituted for one another. Both have different data models.

    • A string for strcat is a null terminated string for which you (as the programmer) guarantee that it has enough space.
    • A string for strncat is a sequence of char that is either terminated at the length you are indicating or by a null termination if it is supposed to be shorter than that length.

    So the use of these functions just depends on the assumptions that you may (or want to) do about your data.

    0 讨论(0)
  • 2021-01-02 12:20

    If you are absolutely sure about source buffer's size and that the source buffer contains a NULL-character terminating the string, then you can safely use strcat when the destination buffer is large enough.

    I still recommend using strncat and give it the size of the destination buffer - length of the destination string - 1

    Note: I edited this since comments noted that my previous answer was horribly wrong.

    0 讨论(0)
  • 2021-01-02 12:29

    Concatenate two strings into a single string.

    Prototypes

    #include <string.h>
    
    char * strcat(char *restrict s1, const char *restrict s2);
    
    char * strncat(char *restrict s1, const char *restrict s2, size_t n);
    

    DESCRIPTION

    The strcat() and strncat() functions append a copy of the null-terminated string s2 to the end of the null-terminated string s1, then add a terminating \0'. The string s1 must have sufficient space to hold the result.

    The strncat() function appends not more than n characters from s2, and then adds a terminating \0'.

    The source and destination strings should not overlap, as the behavior is undefined.

    RETURN VALUES

     The `strcat()` and `strncat()` functions return the pointer s1.
    

    SECURITY CONSIDERATIONS

    The strcat() function is easily misused in a manner which enables malicious users to arbitrarily change a running program's functionality through a buffer overflow attack.

    Avoid using strcat(). Instead, use strncat() or strlcat() and ensure that no more characters are copied to the destination buffer than it can hold.

    Note that strncat() can also be problematic. It may be a security concern for a string to be truncated at all. Since the truncated string will not be as long as the original, it may refer to a completely different resource and usage of the truncated resource could result in very incorrect behavior. Example:

    void
     foo(const char *arbitrary_string)
     {
             char onstack[8] = "";
    
     #if defined(BAD)
             /*
              * This first strcat is bad behavior.  Do not use strcat!
              */
             (void)strcat(onstack, arbitrary_string);        /* BAD! */
     #elif defined(BETTER)
             /*
              * The following two lines demonstrate better use of
              * strncat().
              */
             (void)strncat(onstack, arbitrary_string,
                 sizeof(onstack) - strlen(onstack) - 1);
     #elif defined(BEST)
             /*
              * These lines are even more robust due to testing for
              * truncation.
              */
             if (strlen(arbitrary_string) + 1 >
                 sizeof(onstack) - strlen(onstack))
                     err(1, "onstack would be truncated");
             (void)strncat(onstack, arbitrary_string,
                 sizeof(onstack) - strlen(onstack) - 1);
     #endif
     }
    

    Example

    char dest[20] = "Hello";
    char *src = ", World!";
    char numbers[] = "12345678";
    
    printf("dest before strcat: \"%s\"\n", dest); // "Hello"
    
    strcat(dest, src);
    printf("dest after strcat:  \"%s\"\n", dest); // "Hello, World!"
    
    strncat(dest, numbers, 3); // strcat first 3 chars of numbers
    printf("dest after strncat: \"%s\"\n", dest); // "Hello, World!123"
    
    0 讨论(0)
  • 2021-01-02 12:30

    Static tools are generally poor at understanding the circumstances around the use of a function. I bet most of them just warn for every strcat encountered instead of actually looking whether the data passed to the function is deterministic or not. As already mentioned, if you have control over your input data neither function is unsafe.

    Though note that strncat() is slightly slower, as it has to check against '\0' termination and a counter, and also explicitly add it to the end. strcat() on the other hand just checks for '\0', and it adds the trailing '\0' to the new string by copying the terminator from the second string along with all the data.

    0 讨论(0)
提交回复
热议问题