Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

printf "Modification of a read-only value attempted" error

Tags:

printf

perl

when attempting to print object as in:

print "$response{_content} \n";
printf OUTPUT "$response{_content} \n"; 

The printf statement generates error "Modification of a read-only value attempted"

It's an intermittent error. Only happens once in a while, but this program needs to be 100% reliable. dang.

It prints fine to STDOUT.

What am I doing wrong? arrgh!

like image 504
jebediah Avatar asked May 09 '11 10:05

jebediah


2 Answers

The first argument of printf is interpreted as output format, not output itself. See perldoc -f printf and man 3 printf for details.

The problem is, printf might occasionally try to write to its args (this has even been the source of several vulnerabilities in C programs), for instance:

perl -we 'printf "abc%n\n", $_; print "$_\n";'

As you can see, this sets $_ to 3, which is the number of characters written before %n occurred. Try %n without further args and you'll see the exact error message from OP.

Long story short: use print unless you really need advanced formatting. Keep first argument to printf r/o unless you really need even more advanced formatting.

like image 183
Dallaylaen Avatar answered Sep 22 '22 23:09

Dallaylaen


You will need to inspect stdout for the failures. My guess is that once in a while, $response{_content} contains sequences that have special meaning to printf.

like image 25
Mel Avatar answered Sep 25 '22 23:09

Mel