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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:31:29+00:00 2026-05-19T01:31:29+00:00

This is inspired by this question and the comments on one particular answer in

  • 0

This is inspired by this question and the comments on one particular answer in that I learnt that strncpy is not a very safe string handling function in C and that it pads zeros, until it reaches n, something I was unaware of.

Specifically, to quote R..

strncpy does not null-terminate, and
does null-pad the whole remainder of
the destination buffer, which is a
huge waste of time. You can work
around the former by adding your own
null padding, but not the latter. It
was never intended for use as a “safe
string handling” function, but for
working with fixed-size fields in Unix
directory tables and database files.
snprintf(dest, n, “%s”, src) is the
only correct “safe strcpy” in standard
C, but it’s likely to be a lot slower.
By the way, truncation in itself can
be a major bug and in some cases might
lead to privilege elevation or DoS, so
throwing “safe” string functions that
truncate their output at a problem is
not a way to make it “safe” or
“secure”. Instead, you should ensure
that the destination buffer is the
right size and simply use strcpy (or
better yet, memcpy if you already know
the source string length).

And from Jonathan Leffler

Note that strncat() is even more
confusing in its interface than
strncpy() – what exactly is that
length argument, again? It isn’t what
you’d expect based on what you supply
strncpy() etc – so it is more error
prone even than strncpy(). For copying
strings around, I’m increasingly of
the opinion that there is a strong
argument that you only need memmove()
because you always know all the sizes
ahead of time and make sure there’s
enough space ahead of time. Use
memmove() in preference to any of
strcpy(), strcat(), strncpy(),
strncat(), memcpy().

So, I’m clearly a little rusty on the C standard library. Therefore, I’d like to pose the question:

What C standard library functions are used inappropriately/in ways that may cause/lead to security problems/code defects/inefficiencies?

In the interests of objectivity, I have a number of criteria for an answer:

  • Please, if you can, cite design reasons behind the function in question i.e. its intended purpose.
  • Please highlight the misuse to which the code is currently put.
  • Please state why that misuse may lead towards a problem. I know that should be obvious but it prevents soft answers.

Please avoid:

  • Debates over naming conventions of functions (except where this unequivocably causes confusion).
  • “I prefer x over y” – preference is ok, we all have them but I’m interested in actual unexpected side effects and how to guard against them.

As this is likely to be considered subjective and has no definite answer I’m flagging for community wiki straight away.

I am also working as per C99.

  • 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-19T01:31:29+00:00Added an answer on May 19, 2026 at 1:31 am

    A common pitfall with the strtok() function is to assume that the parsed string is left unchanged, while it actually replaces the separator character with '\0'.

    Also, strtok() is used by making subsequent calls to it, until the entire string is tokenized. Some library implementations store strtok()‘s internal status in a global variable, which may induce some nasty suprises, if strtok() is called from multiple threads at the same time.

    The CERT C Secure Coding Standard lists many of these pitfalls you asked about.

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

Sidebar

Related Questions

Inspired by the comments on this question , I'm pretty sure that Java String
This question is inspired by Jon Skeet's answer: Is there a c# equivalent to
Inspired by this question and answer , how do I create a generic permutations
Somewhat inspired by this question about a graphical programming environment. I don't think that
G'day, Inspired by a comment I made in my answer to this question on
This is inspired by the question OK-Cancel or Cancel-OK? . I remember reading somewhere
This question was inspired by a similar question: How does delete[] know the size
This question is inspired by Does Linux provide a monotonically increasing clock to applications
Inspired by this question , I wanted to try my hand at the latest
Inspired by this question I began wondering why the following examples are all illegal

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.