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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:50:55+00:00 2026-05-13T20:50:55+00:00

I got a problem today. It had a method and I need to find

  • 0

I got a problem today. It had a method and I need to find the problem in that function. The objective of the function is to append new line to the string that is passed. Following is the code

char* appendNewLine(char* str){
    int len = strlen(str);
    char buffer[1024];
    strcpy(buffer, str);
    buffer[len] = '\n';
    return buffer;
}

I had identified the problem with this method. Its kind of straight forward. The method is having a potential of having array’s index out of range. That is not my doubt. In java, I use ‘\n’ for newline. (I am basically a Java programmer, its been many years I’ve worked in C). But I vaguely remember ‘\n’ is to denote termination for a string in C. Is that also a problem with this program?

Please advise.

  • 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-13T20:50:55+00:00Added an answer on May 13, 2026 at 8:50 pm

    Theres quite a few problems in this code.

    1. strlen and not strlent, unless you have an odd library function there.
    2. You’re defining a static buffer on the stack. This is a potential bug (and a security one as well) since a line later, you’re copying the string to it without checking for length.
      Possible solutions to that can either be allocating the memory on the heap (with a combination of strlen and malloc), or using strncpy and accepting the cut off of the string.
    3. Appending ‘\n’ indeed solves the problem of adding a new line, but this creates a further bug in that the string is currently not null terminated.
      Solution: Append ‘\n’ and ‘\0’ to null terminate the new string.
    4. As others have mentioned, you’re returning a pointer to a local variable, this is a severe bug and makes the return value corrupt within a short time.

    To expand your understanding of these problems, please look up what C-style strings are, potentially from here. Also, teach yourself the difference between variables allocated on the stack and variables allocated on the heap.

    EDITed: AndreyT is correct, the definition of length is valid

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

Sidebar

Related Questions

I've got a problem here with an MSI deployment that I'm working on (using
I've got a problem with inheritance and generics. This is the code that illustrates
I've got a problem similar to,but subtly different from, that described here (Loading assemblies
Got a problem with ADOMD.NET 8.0, SQL2008 and our app. It isn't giving us
I got a problem like this (this is html/css menu): Eshop | Another eshop
I've got a problem where I have a .co.uk domain of which I am
On Vista, I got a problem with the application crash handler. Basically, if something
I've got this weird problem - I'm calling ChangeServiceConfig on a newly installed service
I have got the following problem since the server has safe mode turned on,
I've got a little problem with Zend Framework Zend_Pdf class. Multibyte characters are stripped

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.