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

strcmp/strncmp/strnicmp

I have a question about buffer overflows with strcmp or strncmp
(strnicmp). It's best described by providing an example

Example:
There is a string "sBuf" of length 5000. In a for loop from 0 to 5000,
several NUL terminated strings are compared, these strings vary in size
from 3 to 9.

sBuf is user input (a file), memory is dynamically allocated using
malloc (file length+1) and it is properly nul terminated. The other
strings are constants.

Do I need to check if the length of the string fits inside "sBuf"
before doing the compare or will strncmp automatically stop comparing
when it encounters a NUL character in sBuf?

If the character count of sBuf reaches 4998 and the string is of length
6, it would compare unallocated memory if it does not stop at sBuf's
NUL termination.

Because there are many strings to check, I can't simply limit the range
of the for loop to prevent a compare overflow. Checking before each
compare may slow down loops and it may cause bugs due to added
complexity.

Thanks for your answer.

Bert

Nov 14 '05 #1
1 4199
"Bert" <ma***********@gmx.de> wrote:
sBuf is user input (a file), memory is dynamically allocated using
malloc (file length+1) and it is properly nul terminated. The other
strings are constants.

Do I need to check if the length of the string fits inside "sBuf"
before doing the compare or will strncmp automatically stop comparing
when it encounters a NUL character in sBuf?


Both strncmp() and (obviously) strcmp() will stop comparing when they
hit a null character.

Richard
Nov 14 '05 #2

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

Similar topics

2
by: JustSomeGuy | last post by:
What is the equivilent of strnicmp in the stl string class... ie i want to do a case independant comparison of the first n characters.
3
by: Andrej Hocevar | last post by:
Hello, I'm having problems with string comparisons. I have data like char d1 = {1, 2, 3} and char d2 = {1, 2, 3}. Now if I do strcmp(d1, d2), it compares only the first character, it seems. What...
53
by: Allan Bruce | last post by:
Hi there, I am reading a file into a char array, and I want to find if a string exists in a given line. I cant use strcmp since the line ends with '\n' and not '\0'. Is there a similar function...
11
by: Eirik | last post by:
Shouldn't this code work? If not, why shouldn't it? #include <stdio.h> int main(void) { char yesno; char *yes = "yes";
26
by: pembed2003 | last post by:
Hi, I have an application where I use the strncmp function extensively to compare 2 string to see if they are the same or not. Does anyone know a faster replacement for strncmp? I notice there is...
9
by: jacob navia | last post by:
What is the result of that? I am reimplementing the C library, and I would like to know. Now I have it to return zero. Is that correct? The standard says: >> The strncmp function compares...
9
by: Steven | last post by:
Hello, I have a question about strcmp(). I have four words, who need to be compared if it were two strings. I tried adding the comparison values like '(strcmp(w1, w2) + strcmp(w3, w4))', where...
4
by: Preets | last post by:
Hi, Can anybody please tell me the difference between memcmp() and strncmp() functions ?
18
by: jamihuq | last post by:
I'm trying to use strdup, strnicmp and stricmp in an OS that doesn't have an implementation in the OSs string.h function. Does someone have the implementation for these functions and can you please...
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
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.