473,403 Members | 2,284 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

strncpy memory corruption

Hi,

I am writing a small program to basically copy ls. I would like to copy a string so I thought I would use strncpy but I
am getting the following error:

I thought I would be able to debug this but i have been through the man pages and can't figure it out. Doing the same thing with strcpy works fine. The reason I wanted to use strncpy is because it is safe (as far as I am aware) I have seen a few posts to say that it doesn't guarantee adding a '\0' to the end, is this the case? If so how do you copy strings safely?


Expand|Select|Wrap|Line Numbers
  1. *** glibc detected *** ./lscf: malloc(): memory corruption: 0x0804b018 ***
  2. ======= Backtrace: =========
  3. /lib/libc.so.6xb7de56e1]
  4. /lib/libc.so.6xb7de7671]
  5. /lib/libc.so.6(__libc_malloc+0x85)xb7de90c5]
  6. ./lscfx8048a95]
  7. ./lscfx804887a]
  8. /lib/libc.so.6(__libc_start_main+0xdc)xb7d96f9c]
  9. ./lscfx8048671]
  10. ======= Memory map: ========
  11. 08048000-08049000 r-xp 00000000 00:12 3223837912  /home/astro/phrfad/programming/C/lscf/lscf
  12. 08049000-0804a000 r-xp 00000000 00:12 3223837912  /home/astro/phrfad/programming/C/lscf/lscf
  13. 0804a000-0804b000 rwxp 00001000 00:12 3223837912  /home/astro/phrfad/programming/C/lscf/lscf
  14. 0804b000-0806e000 rwxp 0804b000 00:00 0          eap]
  15. b7c00000-b7c21000 rwxp b7c00000 00:00 0
  16. b7c21000-b7d00000 ---p b7c21000 00:00 0
  17. b7d80000-b7d81000 rwxp b7d80000 00:00 0
  18. b7d81000-b7ea9000 r-xp 00000000 03:01 33558130   /lib/libc-2.5.so
  19. b7ea9000-b7eaa000 r-xp 00128000 03:01 33558130   /lib/libc-2.5.so
  20. b7eaa000-b7eac000 rwxp 00129000 03:01 33558130   /lib/libc-2.5.so
  21. b7eac000-b7eaf000 rwxp b7eac000 00:00 0
  22. b7ee1000-b7eeb000 r-xp 00000000 03:01 33561104   /lib/libgcc_s.so.1
  23. b7eeb000-b7eed000 rwxp 00009000 03:01 33561104   /lib/libgcc_s.so.1
  24. b7eed000-b7eef000 rwxp b7eed000 00:00 0
  25. b7eef000-b7ef0000 r-xp b7eef000 00:00 0          dso]
  26. b7ef0000-b7f0b000 r-xp 00000000 03:01 33742551   /lib/ld-2.5.so
  27. b7f0b000-b7f0d000 rwxp 0001a000 03:01 33742551   /lib/ld-2.5.so
  28. bfe1b000-bfe33000 rw-p bfe1b000 00:00 0          tack]
  29. Aborted
  30.  

Expand|Select|Wrap|Line Numbers
  1. .
  2.   char dir_path[BUFSIZ], *newdir;
  3. .
  4. .
  5. /* newdir is malloced here */
  6. .
  7.     //if((strcpy(newdir, dir_path)) == NULL)
  8.     if((strncpy(newdir, dir_path, BUFSIZ)) == NULL)
  9.        fatal(0, "strncpy");
  10. .
  11.  
Apr 25 '08 #1
5 3783
JosAH
11,448 Expert 8TB
Expand|Select|Wrap|Line Numbers
  1. .
  2.   char dir_path[BUFSIZ], *newdir;
  3. .
  4. .
  5. /* newdir is malloced here */
  6. .
  7.     //if((strcpy(newdir, dir_path)) == NULL)
  8.     if((strncpy(newdir, dir_path, BUFSIZ)) == NULL)
  9.        fatal(0, "strncpy");
  10. .
  11.  
I don't see any allocation to 'newdir' at all; you just copy (part of) a string to an
indeterminate 'newdir' value.

kind regards,

Jos
Apr 25 '08 #2
*newdir -: means only pointer. we want to allocate space if want to assign value.

eg:-char *newdir=new char[BUFSIZ]
Apr 25 '08 #3
*newdir -: means only pointer. we want to allocate space if want to assign value.

eg:-char *newdir=new char[BUFSIZ]
sorry, I did allocate for newdir I just forgot to put it in the code snippit:

Expand|Select|Wrap|Line Numbers
  1.     /* copy filename to new string to recurs into later */
  2.     if((newdir = (char *)malloc(strlen(dir_path)+1)) == NULL)
  3.       fatal(0, "malloc");
  4.  
  5.  
Apr 25 '08 #4
weaknessforcats
9,208 Expert Mod 8TB
You are allocating dir_path+1.

You are using strncpy to copy BUFSIZ characters. dir_path is an array of BUFSIZE. Therefore, strncpy has encountered the limit before encountering a null terminator in the source string. Therefore, strncpy does not put a null terminator on your string. Later, when you use dir_path as a string, you will have problems.

How do you copy strings safely in C? You write a function that has the source and destination strings as pointer arguments plus arguments for the source and destination buffer size. Then you copy until the destination is full but not more than the source buffer length incase the source is screwed up.

Then you never use the C library functions anywhere in your code excepot in this function. And I might not even use them there.

Finally, you develop your family of secure string functions.

Or you use Windows where these are already written for you.
Apr 25 '08 #5
Thanks for your reply. I was going to say that it isn't working still, but it is. I changed the size values in the malloc and strncpy for the return value of the snprintf (accounting for \0) function and changed size for snprintf.

I didn't realise the string libraries were so unsafe, I though the *n* functions rectified that!

David


You are allocating dir_path+1.

You are using strncpy to copy BUFSIZ characters. dir_path is an array of BUFSIZE. Therefore, strncpy has encountered the limit before encountering a null terminator in the source string. Therefore, strncpy does not put a null terminator on your string. Later, when you use dir_path as a string, you will have problems.

How do you copy strings safely in C? You write a function that has the source and destination strings as pointer arguments plus arguments for the source and destination buffer size. Then you copy until the destination is full but not more than the source buffer length incase the source is screwed up.

Then you never use the C library functions anywhere in your code excepot in this function. And I might not even use them there.

Finally, you develop your family of secure string functions.

Or you use Windows where these are already written for you.
Apr 28 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Noa Garnett | last post by:
I'm developing on C++, using visual studio 6.0 with service pack 5. I have a memory corruption while debugging. Some of the variables I'm using are suddenly set to zero while progressing along the...
9
by: prabhat143 | last post by:
Hi, I was recently asked to write a function in C that would detect if memory is corrupted. I had no clue about the solution but what I believe is that the solution is not complicated. Does...
4
by: indushekara | last post by:
Hi, We are having memory corruption in our application somewhere, unable to find out. one part of code we found that we are specifying wrong format specifier. Could anyone let me know if the...
3
by: Paminu | last post by:
I get this error after running my application for some time. What does it mean and what should I be looking for in my code?
4
by: Harsha | last post by:
Hi All, There is some memory corruption in my application. It is due to the fact that it is multithreaded. Is this due to some missing mutex locking? Is there any way to find where it is...
4
by: Tomassus | last post by:
Hi there, I have a problem with dynamic memory allocation. I know that it would have been easier to use vectors methods, but i want to know what i do here wrong. This is one of my methods in...
14
by: =?Utf-8?B?UHVjY2E=?= | last post by:
Hi, I'm using VS2005 and .net 2.0. I'm creating an application that has 3 forms. I want allow users to move forward and backward with the forms and retain the data users have entered. I thought...
2
by: Fredo | last post by:
First of all, I apologize for cross-posting. I posted this on the framework.interop group as well, but haven't received a response yet, so I thought I'd try here... I'm trying to write an app...
2
by: Bob Doe | last post by:
I've been told there is memory corruption for buf.str().c_str() Can someone explain why?: void myFunc() { std::stringstream buf; buf << "some string"; const char *data = buf.str().c_str(); ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.