473,387 Members | 1,425 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.

What are the common causes to null-terminated string for not having \0 character and later buffer overflow

Given that the string is of null-terminated type. What could be the
possible causes (by experience) the string to have no null character
(\0)
and cause buffer overflow later. I know it is quite broad, just like to
find out the causes as much as possible so that I could impose stricter
checking toward my codes.

note: I could not use std::string cause it will require a total
rewrite.

thanks.

(this is a re-post from comp.lang.C++ because comp.lang.c seem more
appropriate)

Dec 5 '06 #1
7 2517
semut wrote:
>
Given that the string is of null-terminated type. What could be the
possible causes (by experience) the string to have no null character
(\0)
and cause buffer overflow later.
I know it is quite broad, just like to
find out the causes as much as possible so that
I could impose stricter
checking toward my codes.
How are you aquiring your strings?
What are you doing to them?

--
pete
Dec 5 '06 #2
On 4 Dec 2006 18:19:28 -0800, "semut" <an*****@gmail.comwrote:
>Given that the string is of null-terminated type. What could be the
possible causes (by experience) the string to have no null character
(\0)
and cause buffer overflow later. I know it is quite broad, just like to
The usual suspects: design errors, coding errors, and input errors.
>find out the causes as much as possible so that I could impose stricter
checking toward my codes.
That eliminates one of the design errors.
>
note: I could not use std::string cause it will require a total
rewrite.
And also a change in newgroups. Are you coding in C or C++?
Remove del for email
Dec 5 '06 #3
semut wrote:
Given that the string is of null-terminated type. What could be the
possible causes (by experience) the string to have no null character
(\0)
and cause buffer overflow later. I know it is quite broad, just like to
find out the causes as much as possible so that I could impose stricter
checking toward my codes.
In my experience it is unusual for a string to lack its
terminator. (Strictly speaking, it is impossible: a string,
by definition, has a terminator. Let me say instead that it
is unusual for an array that is "supposed to be a string" to
lack a terminator.)

On those few occasions I've seen it happen, it's almost
always been the result of strncpy(). Some people have come
to believe that strncpy() is "a safer strcpy()," but they
have been deluded. In fact, strncpy() used naively simply
trades one error for a different one: instead of overrunning
the too-small buffer right now, it leaves an unterminated
non-string poison pill for some later victim to choke on.

If non-terminated "string-like things" are actually a
problem in your code base, I think your first step should be
to hunt down every strncpy() call and require the person who
wrote the call to explain just why he did it. (His explanation
may turn out to be satisfactory, but I'd bet against it. I've
heard that some style-checking programs behave this way with
the word "comprised:" they don't bother analyzing the sentence,
but simply flag every "comprised" as incorrect. It's faster
than checking, and nearly always right.)

--
Eric Sosman
es*****@acm-dot-org.invalid
Dec 5 '06 #4
semut wrote:
>
Given that the string is of null-terminated type. What could be
the possible causes (by experience) the string to have no null
character (\0) and cause buffer overflow later. I know it is quite
broad, just like to find out the causes as much as possible so
that I could impose stricter checking toward my codes.
Failure to write the '\0' as the string terminator.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Dec 5 '06 #5
semut wrote:
Given that the string is of null-terminated type. What could be the
possible causes (by experience) the string to have no null character
(\0)
and cause buffer overflow later. I know it is quite broad, just like to
find out the causes as much as possible so that I could impose stricter
checking toward my codes.
Typical causes are copying strings into an array that is shorter than
the source. It might be done with strcpy, strncpy, strcat, sprintf, or
the programmer's own copy loop.

--
Thad
Dec 5 '06 #6

pete wrote:
semut wrote:

Given that the string is of null-terminated type. What could be the
possible causes (by experience) the string to have no null character
(\0)
and cause buffer overflow later.
I know it is quite broad, just like to
find out the causes as much as possible so that
I could impose stricter
checking toward my codes.

How are you aquiring your strings?
What are you doing to them?

--
pete
It is some kind of custom memory allocator which allocate memory based
on a share memory content.

Dec 5 '06 #7
semut said:
Given that the string is of null-terminated type. What could be the
possible causes (by experience) the string to have no null character
(\0)
and cause buffer overflow later.
None. If it doesn't have a null character terminating it, it isn't a string.

When you choose to use a particular data format, it is your responsibility
to ensure that the requirements of that format are met. For example, if you
choose to use a binary search tree, it is your responsibility to ensure
that the tree remains ordered whenever you add, remove, or move nodes. If
you fail to do this, you don't *have* a binary search tree. If you choose
to use a double-linked list, it is your responsibility to ensure that every
node points to its previous node (or NULL if there isn't a previous node)
and its next node (or NULL if there isn't a next node). If you fail to do
this, you don't *have* a double-linked list.

With strings, the requirements are much gentler - all you have to do is
ensure that you don't exceed your memory availability and that you have a
null terminator at the end of the data, taking care to replace it if it
gets erased for some reason (as it might, when copying substrings about or
getting your hands dirty with pointers). But if you fail to do this, you
don't *have* a string.

How many different ways are there to foul up a binary search tree?
Infinitely many. But they all have one shared characteristic: they break
the binary search tree model. How many different ways are there to foul up
a double-linked list? Infinitely many. But they all have one shared
characteristic: they break the double-inked list model.

And how many different ways are there to foul up a string? Infinitely many.
But they all have one shared characteristic: they break the string model.

So the answer is simple: don't break the model.
I know it is quite broad, just like to
find out the causes as much as possible so that I could impose stricter
checking toward my codes.
When writing to a string:

1) Always know your bounds.
2) Never write outside your bounds.
3) Ensure the string is null-terminated before you give up control over it.
note: I could not use std::string cause it will require a total
rewrite.
std::string is just a syntax error, so I don't see how it is relevant.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 5 '06 #8

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

Similar topics

137
by: Philippe C. Martin | last post by:
I apologize in advance for launching this post but I might get enlightment somehow (PS: I am _very_ agnostic ;-). - 1) I do not consider my intelligence/education above average - 2) I am very...
7
by: robertbrown1971 | last post by:
I just inherited a Java application with a fairly complex data model that does not yet have any indexes except those on primary keys. It is still in development and before I get to do any...
16
by: vaughn | last post by:
What is the 'this' keyeword for? If I'm filling a textbox, what's the difference between this.textbox1.Text = "my text"; and textbox1.Text = "my text"; ? I normally use it w/o the 'this'. Thanks.
2
by: Frank van Vugt | last post by:
Hi, Not exactly a showstopper, but I noticed this behaviour: db=# create table f1 (id int, value int); CREATE TABLE db=# insert into f1 select 1 as id, null; INSERT 25456306 1
82
by: quiberon2 | last post by:
Hi, Sorry if it might be a stupid question but what should returns malloc(0) ? void *ptr = malloc(0); I am running gcc 3.3.5 and a non-null address is returned. ( in the compiler that I am...
1
devonknows
by: devonknows | last post by:
Good afternoon, ive got a common dialog which calles the print dialog, it prints perfectly, its prints my ListBox contents right, but when i click cancel on the print screen its just Prints it anyway...
30
by: Bill Reid | last post by:
#define MAX_VALUES 64 typedef struct { unsigned value_1; double value_2; double value_3; double value_4; } VALUES; typedef struct {
4
by: kreuters | last post by:
hey, can anyone explain to me why this method that is called numerous times to read chunks of a file works fine with a normal synchronous read, but causes a large memory leak when BeginRead() is...
1
by: Ben | last post by:
Hi. Can anyone decribe a good method to share Common classes between projects while using source control? I used to just point everything to one directory... but started using SourceSafe...
4
by: Ty | last post by:
Hello all, I am creating a web site with Visual Stuido 2008. I am trying to use a java script file to create a busybox for login from this page http://blogs.crsw.com/mark/articles/642.aspx. I...
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: 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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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
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.