473,387 Members | 1,515 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,387 software developers and data experts.

Under what circumstances will this use of strncpy fail?

Ron
#define MAX_SIZE 512

char mybuffer[MAX_SIZE+1];

void myfunction( const char* src )
{
if( src == null ) { return; }

/* A core dump is occuring here */
strncpy( mybuffer, src, MAX_SIZE );
mybuffer[ MAX_SIZE ] = 0;
}
A core dump is occuring on line 10. If src is not null when I call
strncpy, is there anything about its contents that could cause strncpy
to fail? My guess is that there is no way the contents of src could
cause the core, so the pointer itself is bad, even if it is not null.
However, inspecting the calling code, src looks fine. The only thing
I can't inspect (easily) is the contents of src. I can't think of any
way the contents can be the root cause, but maybe someone reading this
knows better.

Mar 9 '07 #1
9 3150
Ron wrote On 03/09/07 14:44,:
#define MAX_SIZE 512

char mybuffer[MAX_SIZE+1];

void myfunction( const char* src )
{
if( src == null ) { return; }

/* A core dump is occuring here */
strncpy( mybuffer, src, MAX_SIZE );
mybuffer[ MAX_SIZE ] = 0;
}
A core dump is occuring on line 10. If src is not null when I call
strncpy, is there anything about its contents that could cause strncpy
to fail? My guess is that there is no way the contents of src could
cause the core, so the pointer itself is bad, even if it is not null.
However, inspecting the calling code, src looks fine. The only thing
I can't inspect (easily) is the contents of src. I can't think of any
way the contents can be the root cause, but maybe someone reading this
knows better.
What is `null' and where is it defined -- or have
you posted a paraphrase instead of the actual code?

If there really is a `null' floating around, maybe
someone has called myfunction(NULL) and the test fails
to detect it.

But if `null' is really `NULL' and <string.hhas
been #included, I can only guess that `src' points to
someplace bogus, to an address that isn't even part of
your program. Or perhaps to an address within MAX_SIZE
bytes of the end of a chunk of your program's memory,
with no '\0' between it and the edge of the world. You
might also get into trouble if `src' points to a location
somewhere inside `mybuffer'.

Just guessing, I'm afraid ...
--
Er*********@sun.com
Mar 9 '07 #2
Ron
On Mar 9, 3:23 pm, Eric Sosman <Eric.Sos...@sun.comwrote:
>
What is `null' and where is it defined -- or have
you posted a paraphrase instead of the actual code?
Sorry, I've been programming in java alot lately. I meant NULL, and
I'm not suspicious of the validation of the src pointer.
But if `null' is really `NULL' and <string.hhas
been #included, I can only guess that `src' points to
someplace bogus
...
Just guessing, I'm afraid ...
Actually, I appreciate your guesses! I draw the same conclusion -
somehow src must be bad.

Unfortunatley I can't see a problem through inspection. The caller
uses new to allocate an array of char for src. The caller then fills
src with data from a stream, and null terminates src. All of these
operations seem to be properly validated and range checked. I cannot
see any way that memory for src was delete'd or that src was
intentionally reassigned before the call to myfunction. So...

If the value of the src pointer does not change between caller's new
operation and my strncpy, is it possible that the caller could have
successfully used src without causing a core dump there?

If the answer to that is 'no' (and I think it is), I think that means
src was good while the caller was using it, but was unintentionally
changed after that, but before the call to strncpy, such as by a
memory trampling bug or some such. This is the only explanation I can
come up with.

Ahem - I forgot to mention that the problem is intermittent. :) To
give more context, the caller is reading records from a stream, and
myfunction needs to copy one of the strings in the record. Many
records will get processed before the core dump. I have no control
over the code that writes or reads these records, or how src is
initialized or used before it gets to myfunction. But strncpy is
where the core dump happens, so I get to debug. :)

I guess my purpose for posting is to confirm that the only thing that
can go wrong here is the caller gives myfunction a bad pointer (i.e.
myfunction is written correctly and the contents of src don't matter).

Mar 9 '07 #3
Ron wrote:
>
Ahem - I forgot to mention that the problem is intermittent. :) To
give more context, the caller is reading records from a stream, and
myfunction needs to copy one of the strings in the record. Many
records will get processed before the core dump. I have no control
over the code that writes or reads these records, or how src is
initialized or used before it gets to myfunction. But strncpy is
where the core dump happens, so I get to debug. :)
Odds are, something else completely unrelated to your function is doing
something nasty to the head. The problem just happens to reveal its
self in your code.

If your implementation supports it, use a memory bounds checker.

--
Ian Collins.
Mar 9 '07 #4
In article <11**********************@p10g2000cwp.googlegroups .com>,
Ron <bn*****@gmail.comwrote:
>Unfortunatley I can't see a problem through inspection. The caller
uses new to allocate an array of char for src.
There is no such thing as "new" in C. Presumably this is C++.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Mar 9 '07 #5
Ian Collins wrote:
Ron wrote:
>>Ahem - I forgot to mention that the problem is intermittent. :) To
give more context, the caller is reading records from a stream, and
myfunction needs to copy one of the strings in the record. Many
records will get processed before the core dump. I have no control
over the code that writes or reads these records, or how src is
initialized or used before it gets to myfunction. But strncpy is
where the core dump happens, so I get to debug. :)

Odds are, something else completely unrelated to your function is doing
something nasty to the head. The problem just happens to reveal its
self in your code.
"something nasty to the heap"

--
Ian Collins.
Mar 9 '07 #6
Ian Collins said:
Ian Collins wrote:
<snip>
>Odds are, something else completely unrelated to your function is
doing
something nasty to the head. The problem just happens to reveal its
self in your code.
"something nasty to the heap"
Ohhhh, I dunno - I think you got it right the first time. :-)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 9 '07 #7
Richard Heathfield wrote:
Ian Collins said:

>>Ian Collins wrote:


<snip>
>>>Odds are, something else completely unrelated to your function is
doing
something nasty to the head. The problem just happens to reveal its
self in your code.

"something nasty to the heap"


Ohhhh, I dunno - I think you got it right the first time. :-)
Exploding toilets, anyone?

--
Ian Collins.
Mar 9 '07 #8
Ron wrote On 03/09/07 16:46,:
On Mar 9, 3:23 pm, Eric Sosman <Eric.Sos...@sun.comwrote:
> What is `null' and where is it defined -- or have
you posted a paraphrase instead of the actual code?


Sorry, I've been programming in java alot lately. I meant NULL, and
I'm not suspicious of the validation of the src pointer.

> But if `null' is really `NULL' and <string.hhas
been #included, I can only guess that `src' points to
someplace bogus

...
> Just guessing, I'm afraid ...


Actually, I appreciate your guesses! I draw the same conclusion -
somehow src must be bad.

Unfortunatley I can't see a problem through inspection. The caller
uses new to allocate an array of char for src [...]
Aha! You are using That Other Language, the one with
the C-ish-but-not-quite-C rules, the one that puts your
helpless program at the mercy of ravenous "destructors."
You want comp.lang.c++, down the hall to the left.

--
Er*********@sun.com
Mar 9 '07 #9
Ian Collins wrote:
Richard Heathfield wrote:
>Ian Collins said:
>>Ian Collins wrote:

<snip>
>>>Odds are, something else completely unrelated to your function
is doing something nasty to the head. The problem just happens
to reveal its self in your code.

"something nasty to the heap"

Ohhhh, I dunno - I think you got it right the first time. :-)

Exploding toilets, anyone?
Undefined behaviour in the head? Sounds like diarhea.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Mar 10 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: patbaudy | last post by:
Hi, I'm coding a shopping cart in asp. I store all info about ordered items, customer's coordonates, etc...into cookies. When the order is complete I use "CDONTS.NewMail" to send an order...
12
by: ­m½Z | last post by:
I am a C programming beginner... I wonder, why strncpy(s, t, n) does not put '\0' at the end of the string. Because when I output the copied string, it output more than what I want, until I put...
17
by: G Patel | last post by:
E. Robert Tisdale wrote: > > int main(int argc, char* argv) { > quad_t m = {0, 1, 2, 3}; > int r; > fprintf(stdout, "m = ("); > for (size_t...
27
by: smnoff | last post by:
How does( or should user use) strncpy() to allocate enough memory space for the destination string, s1? I guess I am having trouble using strncpy as it seems to be giving me errors. And just...
4
by: chikito.chikito | last post by:
1. Can someone tell me the difference between these two functions: void strcpy(char *s1, const char *s2) { while(*s1++ = *s2++) ; } //function prototype of strcpy follows char...
4
by: lurch132002 | last post by:
i am trying to create an array of structs to hold some information but whenever i get to the second element and try to strncpy it i get a segmenation fault. ive searched around for similar...
2
by: molayos | last post by:
I am getting core dump on strncpy on the following code. I could get strncpy to work only with array and not with pointer. Can somebody take a look at this code and suggest me why its core dump in...
51
by: Ojas | last post by:
Hi!, I just out of curiosity want to know how top detect the client side application under which the script is getting run. I mean to ask the how to know whether the script is running under...
5
by: davidcollins001 | last post by:
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...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...

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.