arthurnn / memcached

A Ruby interface to the libmemcached C client
Academic Free License v3.0
432 stars 125 forks source link

Avoid using out of scope stack allocation #207

Closed jhawthorn closed 2 years ago

jhawthorn commented 2 years ago

Previously the swig-generated code here used an invalid pattern:

  {
    const char *key_ptr;
    size_t key_length_ptr;
    arg2 = &key_ptr;
    arg3 = &key_length_ptr;
  }
  // read and write through the pointers in arg2 and arg3

As key_ptr and key_length_ptr were declared in that scope, once we leave the scope their memory is no longer valid. Address Sanitizer detects and reports this, but fortunately, as far as I can tell, under more "regular" compiler settings there is no difference (I diff'd the assembly output and it was identical) and this happens to work fine as the memory is reserved but not used for other purposes.

The second commit here adjusts extconf to only build the libmemcached/ subdirectory of the vendored libmemcached, skipping tests, clients and others. This made building with ASAN work (otherwise it failed linking some of the other subprojects) and should be a bit faster and more resilient anyways.

arthurnn commented 2 years ago

thanks ❤️