Coping with string contains null byte sent from users
The gsub
method on String
is probably suitable. You can just do string.gsub("\u0000", '')
to get rid of them.
http://ruby-doc.org/core-2.1.1/String.html#method-i-gsub
ArgumentError: string contains null byte when writing Marshalled data into a file
The order of the file name and content argument is reversed. The first argument must be the name and the second one the content. The argument error is raised because file names shouldn't contain null bytes.
And since you're dealing with binary data, you should use IO.binwrite
:
File.binwrite "users.txt", Marshal.dump(users)
Idiomatic way to include a null character / byte in a string in OCaml
You can use the generic "hexadecimal code" escape sequence to write the null byte (or any other byte you want):
let null_byte = '\x00';;
let sketchy_string = "/bin/ls\x00";;
For further reference, see the section of the Ocaml manual covering escape sequences: http://caml.inria.fr/pub/docs/manual-ocaml/lex.html#escape-sequence
How to escape a NULL byte as an argument to a shell command inside a Makefile
Due to the execve(2) semantics it is not possible to pass a string containing a null byte as argument. Each argument string is terminated by null byte, therefore making it impossible to distinguish between the contained null byte and the end of the string.
How could I copy data that contain '\0' character
Your my_strcpy
is working fine, when you write a char*
to cout
or calc it's length with strlen
they stop at \0
as per C string behaviour. By the way, you can use memcpy
to copy a block of char
regardless of \0
.
User typing null terminator in scanf
On many systems, the answer is "Yes".
Usually, the magic sequence is Control-@.
The character code for @ is usually 64, one less the that of A (65). Control-A is character code 1; one less is character code 0, aka '\0'
.
Note that to get a zero-length input, you'd have to type the null byte as the first non-space character, and you would still need to hit return to enter the input. Your program would find it hard to reliably identify what else was entered after the null byte.
Null byte and arrays in C
Seems like you are confused with arrays and strings.
When you declare
char letters[10] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
then it reserves only 10 contiguous bytes in a memory location.
2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 //memory addresses. I assumed it is to be starting from 2000 for simplification.
+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| | | | | | | | | | |
| '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' |
| | | | | | | | | | |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
In C indexing starts from 0
. You can access your allocated memory location from letters[0]
to letters[9]
. Accessing the location letters[10]
will invoke undefined behavior. But when you declare like this
char *letters = "0123456789";
or
char letters[11] = "0123456789";
then there are 11 bytes of space are allocated in memory; 10 for 0123456789
and one for \0
(NUL character).
2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 //memory addresses. I assumed it is to be starting from 2000 for simplification.
+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-------+
| | | | | | | | | | | |
| '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' | '\0' |
| | | | | | | | | | | |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-------+
^
| NUL character
Take another example
#include <stdio.h>
int main(){
char arr[11];
scanf("%s", arr);
printf("%s", arr);
return 0;
}
Input:
asdf
Output:
asdf
Now have a look at memory location
+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-------+
| | | | | | | | | | | |
| 'a' | 's' | 'd' | 'f' |'\0' | | | | | | |
| | | | | | | | | | | |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-------+
Related Topics
Determining Whether One Array Contains the Contents of Another Array in Ruby
Generating Sequential Numbers in Multi-User Saas Application
Compute Geo Distance in Elasticsearch
Implicit Argument Passing of Super from Method Defined by Define_Method() Is Not Supported
Carrierwave: Create 1 Uploader for Multiple Types of Files
Celluloid Async Inside Ruby Blocks Does Not Work
How to Break Out of a Map/Collect and Return Whatever Has Been Collected Up to That Point
Port in Use When Not Using a Port
How to Declare a Rake Task That Depends on a Parameterized Task
How to Properly Test Cancan Abilities with Rspec
Ruby Imap "Changes" Since Last Check
How Does Rake Db::Migrate Actually Work
When to Use Self in Module's Methods
Stub Method Only on The First Call with Rspec
How to Retrieve Exif Information of an Image in Rails