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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:41:54+00:00 2026-06-02T11:41:54+00:00

Is strncpy() secure for iPhone development? If not, what is a better String API

  • 0

Is strncpy() secure for iPhone development?

If not, what is a better String API to use that is recommended to be secure?

  • 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-02T11:41:58+00:00Added an answer on June 2, 2026 at 11:41 am

    If you know the limitations of strncpy(), then it is OK. I avoid it because I don’t like its limitations, which are two-fold:

    • It does not guarantee null termination
    • It always writes to every byte of the target buffer

    This means that if you write:

    char little[10];
    char large[20480];
    
    strncpy(little, sizeof(little), "abcdefghijklmnopqrstuvwxyz");
    strncpy(large,  sizeof(large),  "abcdefghijklmnopqrstuvwxyz");
    

    Then little is not a null-terminated string, though no buffer overflow has occurred, and large had 20454 nulls copied to its tail end. Both are troublesome.

    • Consider whether strlcpy() and strlcat() are available on iOS; they are on Mac OS X.

    If you’re coding in C++, you shouldn’t be using C strings at all, or only in the most limited circumstances where a system service requires the use, and then you should have a cover function (inline) that takes a C++ string and passes the somestring.c_str() value to the system service.

    If you’re coding in Objective-C, you’ll use the NS* strings.

    So, only consider strncpy() if you are coding in C. Treat it with caution even so.

    I have a thesis (and I don’t lay any claim to novelty in it — I collected the idea from others):

    • You can only use functions like strcpy(), strncpy(), strcat() and strncat() safely if you know the lengths of the strings, both the target buffers and the source strings (and you know the foibles of the function you’re calling — quickly, what does the length passed to strncat() represent?(1)).
    • If you know how long everything is, you don’t need to use functions like strcpy(); you can use memmove() (or memcpy()).
    • So the string copy and move functions should be irrelevant; you don’t need them in safe code because you know how long everything is and can therefore use the memory routines instead.

    (1) The length is the space available in the target buffer after the current string is accounted for. So, to be able to use strncat(), you must know how long the string is in the target string, and the total length available, so that you can have strncat() skip over the initial segment of the string and then concatenate some or all of the second string. But, if you know that, you could have used:

    strncpy(target + curr_target_strlen, source, target_size - curr_target_strlen);
    

    which would be ‘more efficient’ because it doesn’t involve skipping over the leading part of the string (which, incidentally, can lead to quadratic behaviour if you’re building a long string with lots of strncat() or strcat() operations). Or, given that you know all the sizes, you could use memmove():

    size_t copy_length = target_size - curr_target_strlen;
    if (copy_length > source_strlen)
        copy_length = source_strlen + 1;
    memmove(target + curr_target_strlen, source, copy_length);
    

    And, unless I’ve got an off-by-one error in code written on the spur of the moment, that avoids most of the problems with strncat(). If you always use strncat() with the first argument pointing to the null at the end of a string, it has its uses (and may be optimized in assembler. Otherwise, it is not a good choice — IMNSHO.

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

Sidebar

Related Questions

For doing string concatenation, I've been doing basic strcpy , strncpy of char* buffers.
Possible Duplicate: Why does this intentionally incorrect use of strcpy not fail horribly? Below
I have create a wrapper for the strncpy function. I will have to use
The following is in C++. I have a string that contains the environment variables
I wanted to create a string padding function for the use of left-padding a
#define __HAVE_ARCH_STRCPY What's the meaning of __HAVE_ARCH ? I'm not a native speaker and
i have been reading about a function that can overwrite its return address. void
Is there an exact equivalent to strncpy in the C++ Standard Library? I mean
How would I manually concatenate two char arrays without using the strncpy function? Can
gcc 4.4.4 c89 My program does a lot of string coping. I don't want

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.