0

I am working in PHP 7, I have a case in which i want to pass a string of length 0. so how can we generate zend_string variable of length 0.

I tried assigning NULL to zend_string variable but down the line code crashed because it is trying to dereference NULL. so I am guessing we need to allocate memory to zend_string variable and whole value NULL or string length is 0.

NikiC
  • 100,734
  • 37
  • 191
  • 225
abhi7436
  • 43
  • 6

1 Answers1

0

A quick scan of the source shows a few examples of this:

zend_string *str;
str = zend_string_alloc(sizeof("") - 1, 0);
Alex Howansky
  • 50,515
  • 8
  • 78
  • 98
  • 4
    Unless you actually do need a new allocation, the correct way is using `ZSTR_EMPTY_ALLOC()`, which will return an interned string. (The name is bad, it does not allocate anything.) – NikiC Apr 15 '16 at 14:55
  • Nice, thanks, my answer was just a grep and guess. :) – Alex Howansky Apr 15 '16 at 15:24
  • I tried above both methods but did not solved my issue. I'll Explain scenario again. Earlier in PHP 5 zval->value.str used to be a char array but in php7 it is modified to zend_string* type. My requirement is zend_string* variable should have the length of certain value let say 5 but its value should be NULL. – abhi7436 Apr 18 '16 at 08:22
  • 1
    That's abusive, surely you can find another way. If you can't allocate the string as 5 bytes long (zend_string_alloc(5, 0)), and null terminate at the first byte (ZSTR_VAL(s)[0] = 0). – Joe Watkins Apr 24 '16 at 14:06