Skip to content

Commit edab9ad

Browse files
committed
Fix #81400: Unterminated string in dns_get_record() results
If we assemble a zend_string manually, we need to end it with a NUL byte ourselves. We also fix the size calculation for that zend_string; there is no need for the extra byte for each part, and we don't have to multiply by two, since we're using DnsQuery_A(), not DnsQuery_W () (in which case we would have to do the character set conversion, anyway). This avoids over-allocation, and the need to explicitly set the string length. Finally, we use the proper access macro for zend_strings. Closes GH-7427.
1 parent eda9f5f commit edab9ad

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ PHP NEWS
1818

1919
- Standard:
2020
. Fixed bug #71542 (disk_total_space does not work with relative paths). (cmb)
21+
. Fixed bug #81400 (Unterminated string in dns_get_record() results). (cmb)
2122

2223
- SysVMsg:
2324
. Fixed bug #78819 (Heap Overflow in msg_send). (cmb)

ext/standard/dns_win32.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,18 +223,18 @@ static void php_parserr(PDNS_RECORD pRec, int type_to_fetch, int store, int raw,
223223
array_init(&entries);
224224

225225
for (i = 0; i < count; i++) {
226-
txt_len += strlen(data_txt->pStringArray[i]) + 1;
226+
txt_len += strlen(data_txt->pStringArray[i]);
227227
}
228228

229-
txt = zend_string_safe_alloc(txt_len, 2, 0, 0);
230-
txt_dst = txt->val;
229+
txt = zend_string_alloc(txt_len, 0);
230+
txt_dst = ZSTR_VAL(txt);
231231
for (i = 0; i < count; i++) {
232232
size_t len = strlen(data_txt->pStringArray[i]);
233233
memcpy(txt_dst, data_txt->pStringArray[i], len);
234234
add_next_index_stringl(&entries, data_txt->pStringArray[i], len);
235235
txt_dst += len;
236236
}
237-
txt->len = txt_dst - txt->val;
237+
*txt_dst = '\0';
238238
add_assoc_str(subarray, "txt", txt);
239239
add_assoc_zval(subarray, "entries", &entries);
240240
}

0 commit comments

Comments
 (0)