Stringsand BufferOverflows
Stringsand BufferOverflows
#include <stdio.h>
#include <string.h>
int main()
{
char *s1="opengenius";
char *s2="openjeniu8";
printf("%d\n",strcmp(s1,s2));
printf("%d\n",strncmp(s1,s2,9));
printf("%d\n",strncmp(s1,s2,4));
char t1[]={'o','p','e'};
char t2[]={'o','p','e'};
printf("%d\n",strcmp(t1,t2));
printf("%d\n",strncmp(t1,t2,3));
printf("%d\n",strncmp(t1,t2,2));
printf("%d\n",strncmp(t1,t2,4));
return 0;
}
Strcat Vs Strncat
1) Use snprintf() which always appends null character to the end of string but if there is
no space output will be truncated.
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
int main(){
char mystr[5];
snprintf(mystr, sizeof(mystr),"%s","apple");
printf("%s",mystr);
return 0;
2) The strlcpy() and strlcat() functions copy and concatenate strings with the same input
parameters and output result as snprintf() means guarantee NUL-termination if there is
room.
3) Always ensure input strings are within expected lengths before processing them.
4) Consider using dynamic memory allocation (eg: malloc())
5) Use safe functions like strcat_s, strcpy_s(), strncat_s, strncpy_s etc..(But GCC (or
rather, glibc) does not support this. MSVC includes those functions also safe C
libraries).
6) In case of strcpy_s(), function copies characters from a source string to a destination
character array up to and including the terminating null character.
The strcpy_s() function succeeds only when the source string can be fully copied to the
destination without overflowing the destination buffer. The function returns 0 on
success, Otherwise, a nonzero value is returned. Good than strlcpy() because no
truncation…
7) Below Figure 1 shows the build error (in MSVC) while building the program with
strcpy() and recommend using strcat_s(). In later versions of Visual Studio, additional
security checks (CERT) are enabled by default on new projects, which makes the
warning you see be treated as an error. But in Figure 2 no build error (in MSVC)
because strcpy() is commented. Figure 3 shows while running Line number 17 no room
to store in the destination. You can disable the CERT warnings you saw by changing
the properties as follows. Right-click on Project-> select Properties. In the Property
pages go to C/C++-> Preprocessor -> add _CRT_SECURE_NO_WARNINGS to the
preprocessor definitions. Figure 4 shows the settings before adding the definition.
Figure 5 and 6 show settings after adding the definition and the output without any build
error while using strcpy() respectively.
Figure 1
Figure 2
Figure 3
Figure 4
Figure 5
Figure 6
Wide characters
Wide characters are like character data. The main difference is that char takes 1-byte space,
but a wide character takes 2-bytes (sometimes 4-byte depending on the compiler) of space
in memory. For 2-byte space wide characters can hold 64K (65536) different characters.
So the wide char can hold UNICODE characters. The UNICODE values are international
standard that allows for the encoding of characters virtually for any character of any
language.
#include<iostream>
#include<string>
int main() {
wcout << "The wide characters and its size: " << endl;
wstring wstr[N];
L"\"): "
L"\"): "
L" \u01c4 \u01c5 \u01c6 \u01c7 \u01c8 \u01c9 \u01ca \u01cb \u01cc";
L"\"): "
wsncopy
Copies the first num characters of source to destination. If the end of the source C wide
string (which is signaled by a null wide character) is found before num characters have
been copied, destination is padded with additional null wide characters until a total
of num characters have been written to it. No null wide character is implicitly appended at
the end of destination if source is longer than num (thus, in this case, destination may not
be a null terminated C wide string).
#include <wchar.h>
int main ()
wchar_t wcs2[40];
wchar_t wcs3[40];
wprintf (L"%ls\n%ls\n%ls\n",wcs1,wcs2,wcs3);
return 0;