ss_convert_to_wide returns non NUL-terminated SIZED_STRING when input is empty
bug
Hi, the function `ss_convert_to_wide` does not append a null terminator to the `c_str`.
The docs specify the string should be null terminated along with the size:
https://github.com/VirusTotal/yara/blob/0f0c667c054979dafeca43149930eaf0e67b0dbb/libyara/include/yara/sizedstr.h#L50-L52
As a result, this causes use of uninitialized memory and/or overflows in downstream code.
Tested on the most recent commit `0f0c667c`.
(found via automated fuzzing)
The following testcase demonstrates the issue. (the second `abort()` triggers)
```cpp
#include <cstdio>
#include <cstdlib>
#include <cstdint>
#include <cstring>
extern "C" {
#include "yara/sizedstr.h"
}
int main(){
// Create an empty SIZED_STRING
SIZED_STRING* s = ss_new("");
if(!s) return 0;
// Convert to wide
SIZED_STRING* w = ss_convert_to_wide(s);
if(!w) return 0;
// Expect NUL at position length for both
if (s->c_string[s->length] != '\0') abort();
if (w->c_string[w->length] != '\0') abort(); // Fails: w->length==0 but w->c_string[0] != '\0'
return 0;
}
```
0 条评论