Regardless of its maximum value, you have a bigger problem there; from the POSIX spec:
The dd
utility shall copy the specified input file to the specified output file with possible conversions using specific input and output block sizes. It shall read the input one block at a time, using the specified input block size; it shall then process the block of data actually returned, which could be smaller than the requested block size.
(emphasis added)
As I wrote in the past, dd
is an extremely stupid tool: in your case, it essentially boils down to
char *buf = malloc(bs);
for(int i = 0; i < count; ++i) {
int len = read(STDIN_FILENO, buf, bs);
if(len == 0) break;
write(STDOUT_FILENO, buf, len);
}
free(buf);
bs
is just the argument dd
uses to perform the read(2)
syscall, but read(2)
is allowed to perform a "short read", i.e. to return less bytes than requested. Indeed, it's what it does if it has some bytes available right now, even if they aren't all you asked for; this is typical if the input file is a tty, a pipe or a socket (so you are particularly at risk with your CGI...). Just try:
$ dd bs=1000 count=1
asd
asd
0+1 records in
0+1 records out
4 bytes copied, 1.75356 s, 0.0 kB/s
Here I typed in asd
and pressed enter; dd
read it (performing a single read(STDIN_FILENO, buf, 1000)
and wrote it out; it did one read
as requested, so it exits. It doesn't look like it copied 1000 bytes.
Ultimately, plain "standard" dd
is a way too stupid tool for most needs; you can wrangle it to do what you need by either:
- by using
bs=1
and using count
for the number of bytes; this is guaranteed to copy the number of bytes you need (if available before EOF), but it's quite inefficient, as it performs one syscall per byte;
- add the
fullblock
flag; this makes sure that dd
accumulates a full input block before writing it out. Notice however that this is nonstandard (GNU dd has it, IDK about others).
Ultimately, if you are going for non-POSIX extension, my suggestion is to just use head -c
: it will do the Right Thing with sensible buffering and no particular size limits, ensuring correctness and good performance.
$CONTENT_LENGTH
is, thatdd
command may read just a single byte. If successful.