How would I interpret /dev/urandom in a stdout safe way and in a printf "..." >file safe way that will not result in any errors in printf such as invalid characters due to incorrectly parsed input.
Note that the bytes should be parsed as closely to there meaning as possible in the case that it is \xxx byte wich in most cases it is although it just spews out random bytes anyway.
Note it is guaranteed that /dev/urandom will produce a new line \n somewhere in its output wich can be used to limit the output if needed.
This is my current code wich uses strace source code to help interpret the bytes correctly although it is not perfectly printf safe however it does (from testing) appear to be at least stdout safe (although it does sometimes seg fault at least 10 in 20 of every 100 iterations per 100 lines read (for 100 ... readrandom 100 /dev/urandom ; done),
an extensive test of 1 line read per loop (as reading more then one line produces seg faults) :
unset segfaults
unset timeouts
for ((o=0; o<100; o++))
do
for ((i=0; i<1000; i++))
do
timeout 2 ./readrandom 1 /dev/urandom
ret=$?
if [[ $ret == 139 ]]
then
segfaults+=(139)
elif [[ $ret == 124 ]]
then
timeouts+=(124)
fi
done
done
echo "segfaults recieved: ${#segfaults[@]} $(declare -p segfaults >/dev/null 2>&1)"
echo "timeouts recieved: ${#timeouts[@]} $(declare -p timeouts >/dev/null 2>&1)"
[output not included]
segfaults recieved: 2
timeouts recieved: 30
Aucun commentaire:
Enregistrer un commentaire