Ulrich Drepper (udrepper) wrote,

So close, but no cigar

It's nice to see some people actually look at their DSO's and rewrite them to not be resource hogs. One late example is this PCRE code and the optimization done by one Marco Barisione who should be applauded for starting the work. But then this:

const char *_pcre_ucp_names =
  "Any\0"
  "Arabic\0"
  "Armenian\0"
  ...
  "Zs";


This is a global variable. Anybody seeing what is wrong?

What this does is define a variable in .data (it's modifiable) which points to a constant string. This means


  1. An additional variable

  2. More attack points, the variable is writable

  3. An additional relocation

  4. Getting the string address requires a memory load and accessing the string itself requires two memory loads



People, think before writing code! All that is needed here is name for the memory area containing the constant string. I.e.:

const char _pcre_ucp_names[] =
  "Any\0"
  "Arabic\0"
  "Armenian\0"
  ...
  "Zs";


See the difference? This one character removed and two added make all the difference in the world. The binary is smaller (at least 32 bytes on x86-64, more counting the simpler memory access in the actual code), one less relative relocation, faster code at runtime since the code to compute the string address needs no memory access.
Tags: programming
  • Post a new comment

    Error

    Comments allowed for friends only

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded  

  • 3 comments