bzero doesn't do what it says it does - c

I use vscode as my code editor, and in the header file <strings.h> there is a function called bzero and when hovering on the function vscode says that bzero Set N bytes of S to 0. But I don't think it works like that. I created an array of 11 chars which called s and placed inside it Hello World.
Then I used bzero to set the first 4 bytes of s to 0, but from the output it seems like it cleaned the whole buffer.
#include <strings.h>
#include <stdio.h>
int main(int argc, char const *argv[])
{
char s[11] = "Hello World";
bzero(s, 4);
puts(s);
return 0;
}
$ cc main.c -o main && ./main
# empty
$

bzero does exactly what it says. The issue you’re facing is due to a misunderstanding of what a string is in C.
Briefly, a C string is a zero-terminated buffer of chars. That is, C treats an array of chars as a string by considering all chars until it finds the first one whose value is 0.
puts (and printf etc.) uses this definition of “string”.
As a consequence, setting even just the first char in the array to 0 results in an empty string, regardless of what comes after.
(Note also that bzero is a legacy function and its use is discouraged; use memset instead.)

Zero is a string-terminating character. Therefore if you set the first byte of your string to zero, puts will believe that the string is empty.

puts() expects a pointer to a string. Strings in C is sequences of characters *terminated by a null-character ('\0'). Null-character is represented by a value zero.
Therefore, puts() stops at the first zero and prints an empty string.
Print the whole buffer to see the effect of bzero().
#include <strings.h>
#include <stdio.h>
int main(int argc, char const *argv[])
{
char s[11] = "Hello World";
bzero(s, 4);
for (int i = 0; i < 11; i++) printf("%d ", s[i]); // print elements of the buffer
puts(s);
return 0;
}
Output:
0 0 0 0 111 32 87 111 114 108 100

Related

CS50 IDE: printf returns extra characters

I am having problems with the printf function in the CS50 IDE. When I am using printf to print out a string (salt in this code), extra characters are being output that were not present in the original argument (argv).
Posted below is my code. Any help would be appreciated. Thank you.
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
int main(int argc, string argv[])
{
// ensuring that only 1 command-line argument is inputted
if (argc != 2)
{
return 1;
}
char salt[2];
for (int i = 0; i < 2; i++)
{
char c = argv[1][i];
salt[i] = c;
}
printf("the first 2 characters of the argument is %s\n", salt);
}
You are missing a string terminator in salt.
Somehow the computer needs to know where your string ends in memory. It does so by reading until it encounters a NUL byte, which is a byte with value zero.
Your array salt has exactly 2 bytes of space, and after them, random garbage exists which just happens to be next in memory after your array. Since you don't have a string terminator, the computer will read this garbage as well until it encounters a NUL byte.
All you need to do is include such a byte in your array, like so:
char salt[3] = {0};
This will make salt one byte longer, and the {0} is a shorthand for {0, 0, 0} which will initialize the contents of the array with all zerores. (Alternatively, you could use char salt[3]; and later manually set the last byte to zero using salt[2] = 0;.)
In your case, salt is at least one element shy of being a string, unless the argv[1] is only one element, it does not contain a null-terminator.
You need to allocate space to hold the null-terminator and actually put one there to be able to use salt as string, as expected for the argument to %s conversion specifier in case of printf().
Otherwise, the string related functions and operations, which essentially rely on the fact that there will be a null terminator to mark the end of the char array (i.e., mark the end of valid memory that can be accessed), will try to access past the valid memory which causes undefined behavior. Once you hit UB, nothing is guaranteed.
So, considering the fact that you want to use
"....the first 2 characters of the argument....."
you need to make salt a 3-element char array, and make sure that salt[2] contains a null-terminator, like '\0'.

lack of understanding about sscanf usage

I would like to parse a specific line. So, I wrote the following piece of code in order to test the logic but I probably understand something wrongly :
typedef struct vers
{
char tu8UVersion[5];
char tu8UCommit[32];
}tst_prg_versions;
int main(int argc, char **argv)
{
tst_prg_versions lstVer;
char buf1[32];
char buf2[32];
char str[] = "BOARD-VERS-v1.0.0-git+9abc12345a";
sscanf(str, "BOARD-VERS-v%5s-git+%s", lstVer.tu8UVersion, lstVer.tu8UCommit);
printf("vers='%s'\n", lstVer.tu8UVersion);
printf("commit='%s'\n", lstVer.tu8UCommit);
sscanf(str, "BOARD-VERS-v%5s-git+%s", buf1, buf2);
printf("vers='%s'\n", buf1);
printf("commit='%s'\n", buf2);
return 0;
}
Once executed it returns :
vers='1.0.09abc12345a'
commit='9abc12345a'
vers='1.0.0'
commit='9abc12345a
Why the first vers is equals to 1.0.09abc12345a and not 1.0.0 ?
The first actually reads 1.0.0! Problem is, however, that tu8UVersion is not null-terminated, thus printf (not sscanf) prints beyound the field (doing so is undefined behaviour, however, as noted by sjsam) - which is immediately followed by tu8UCommit (does not necessarily have to be so, there could still be some fill bytes in between for alignment reasons!).
You need to either print 5 characters at most (%.5s in printf format string) or leave place for terminating the tu8UVersion with 0, as proposed in a comment already.
Something similar could have happened with your buffers, too. You are lucky that they appearently have been initialized to 0 already (probably because of compiled as debug version), which again does not necessarily have to happen. So with bad luck, you could have printed the whole rest of buf1 (having been left at garbage) and even beyond.
Why the first vers is equals to 1.0.09abc12345a and not 1.0.0 ?
Remember that you have
typedef struct vers
{
char tu8UVersion[5];
char tu8UCommit[32];
}tst_prg_versions;
I guess, there is a good chance the memory for tu8UVersion and tu8UCommit is contiguous. Since you have not null-terminated tu8UVersion when you do :
printf("vers='%s'\n", lstVer.tu8UVersion);
it goes on to print tu8UCommit and it stops because tu8UCommit is null terminated.
While sscanf seem the most sensible solution here you could also introduce some formatting :
char tu8UVersion[32];
/* version number can't get too big.
* So the first step is do allocated a
* reasonably - but not too - big size for it.
* So that you can be sure there are few empty bytes at the end.
*/
and then use a function to sanitize a string :
char* sanitized(char* ptr)
{
if(ptr[strlen(ptr)]!='\0') // include string.h for strlen
ptr[strlen(ptr)]='\0';
return ptr;
}
and print it like :
printf("vers='%s'\n", sanitized(lstVer.tu8UVersion));
Your problem has already been identified in the comments: You don't leave space for the terminating null character and the two strings are run together.
If you want to scan a version whose size you don't know beforehand, you can limit the characters to scan to decimal digits and points with %[.-9] or to everything except a hyphen with %[^-]. (The %[...] format is like %s, except that you must provide a list of valid characters in the brackets. A caret as first letter means that the string is made up of characters that are not listed. In other words, %s is short for %[^ \t\n]
When you scan a string, you should test the return value of sscanf to be sure that all items have been scanned correctly and contain valid values.
Here's a variant that scans version numbers of up to 11 letters:
#include <stdlib.h>
#include <stdio.h>
typedef struct vers
{
char tu8UVersion[12];
char tu8UCommit[32];
} tst_prg_versions;
int main(int argc, char **argv)
{
tst_prg_versions lstVer;
char str[] = "BOARD-VERS-v1.0.0-git+9abc12345a";
int n;
n = sscanf(str, "BOARD-VERS-v%11[^-]-git+%s",
lstVer.tu8UVersion, lstVer.tu8UCommit);
if (n == 2) {
printf("vers='%s'\n", lstVer.tu8UVersion);
printf("commit='%s'\n", lstVer.tu8UCommit);
} else {
puts("Parse error.");
}
return 0;
}

clear the last 5 characters from a string using memset

I have to clear the last 5 characters from a string using memset in c;
I know only to set the first characters from a string using this function.
For example:
#include <stdio.h>
#include <string.h>
int main ()
{
char str[] = "almost every programmer should know memset!";
memset (str,'-',6);
puts (str);
return 0;
}
Thank you!
Use:
char str[] = "EVERY C programmer should know memset!";
memset (&str[strlen(str) - 5],'-',5);
This will overwrite the last 5 characters with -.
Beware that the string hast to have at least 5 characters for this to work correctly.

Need help finding bug, if string input is composed all of same character one output character is corrupt

reverser() reverses a cstring (not in place). 99% of the time it works but some input corrupts it for example it appears if aStr2[] is assigned a string made up of the same character it will have an error.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* reverser(const char *str);
int main()
{
char aStr[] = "aaa";
char aStr2[] = "cccccc";
printf("%s %s", aStr, aStr2);
char* tmp = reverser(aStr2);//tmp now has garbage
printf("\n%s", tmp);
printf(" %s", aStr2);
return 0;
}
char* reverser(const char *str)
{
char* revStr = (char*)malloc(strlen(str));
int i;
for(i = strlen(str)-1; i >= 0; i--)
{
revStr[strlen(str)-1-i] = str[i];
}
return revStr;
}
Gives
aaa cccccc
cccccc9 cccccc
Process returned 0 (0x0) execution time : 0.068 s
Press any key to continue
Notice the 9 that shouldn't be there.
Change this malloc to strlen(str) + 1 , plus 1 for '\0'
char* revStr = (char*)malloc(strlen(str) + 1);
and after the for loop
revStr[strlen(str)+1] = '\0';
Your problem is that you don't put the string terminator in your reversed string. All strings in C are actually one extra character that isn't reported by strlen, and that is the character '\0' (or plain and simple, a zero). This tells all C functions when the string ends.
Therefore you need to allocate space for this extra terminator character in your malloc call, and add it after the last character in the string.
There are also a couple of other problems with your code, the first is that you should not cast the return of malloc (or any other function returning void *). Another that you have a memory leak in that you do not free the memory you allocate. This last point doesn't matter in a small program like the one you have here, but will be an issue in larger and longer running programs.
You haven't null-terminated your reversed string. You need to set the final index of revStr[] to 0.

strange read buffer

here i meet a strange problem about c read function in linux.
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
int main(int argc, char** argv){
int fd=open("a.c",O_RDONLY);
if(fd==-1){
fprintf(stderr,"%s\n",strerror(errno));
}
char buf[10];
if(read(fd,buf,9)==-1){
fprintf(stderr,"%s\n",strerror(errno));
}else{
printf("%s\n",buf);
}
}
i think the buf should be initialize to zero, so the first 9 char read to buffer and the last one is '\0' and it like a string. but the resule is odd, below is a.c file and the result of this program,
a.c
1234567890abcd
result
1234567893øþzôo`
seems this string is out of buffer, I can't figure out what happened, can anyone help me?
thanks.
When you print a character array without ending '\0', printf will print all characters till it finds '\0' in the memory. In this case, looks like '1234567893øþzôo` is followed by '\0'. Note that printf does not know the size of 'buf' array, hence it will print even those characters present after the end of buf array.
As you have suggested it is better to either set entire buffer to 0 or add '\0' explicitly at the end (as shown in below code).
buf[9] = '\0';
You said "i think the buf should be initialize to zero". The compiler does not do this automatically for you, so you will need to do it yourself if that is what you want:
char buf[10];
memset(buf, 0, sizeof(buf));
Before the buffer is initialized, you have no guarantees on what its contents will be.
ISTM your buffer is not zero-terminated, since you only read 9 characters. Change the last part of your code:
if(read(fd,buf,9)==-1){
fprintf(stderr,"%s\n",strerror(errno));
}else{
/* add this */
buf[9] = '\0';
printf("%s\n",buf);
}
}
What happens if you add that?
You should initialize buf to all 0.

Resources