diff options
author | Paweł Redman <pawel.redman@gmail.com> | 2017-04-07 12:34:39 +0200 |
---|---|---|
committer | Paweł Redman <pawel.redman@gmail.com> | 2017-04-07 12:34:39 +0200 |
commit | 397c902d7eb8c250bbc480f826cc873e287f94c7 (patch) | |
tree | f50cf72f86211e9adb53adf2b5395d4c09b23f4f | |
parent | 6f4faecf91e08d7684030b3b28cef706e5296b21 (diff) |
Fix a potential NULL pointer dereference.
-rw-r--r-- | src/lexer.c | 5 | ||||
-rw-r--r-- | src/shared.h | 2 |
2 files changed, 5 insertions, 2 deletions
diff --git a/src/lexer.c b/src/lexer.c index 5863ef6..a3d5b61 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -85,8 +85,11 @@ char *vstr_strdup(vstr_t *vstr) return str; } -char *vstr_to_cstr(vstr_t *vstr) +const char *vstr_to_cstr(vstr_t *vstr) { + if (!vstr->size) + return NULL; + vstr->data[vstr->size] = '\0'; return vstr->data; } diff --git a/src/shared.h b/src/shared.h index bf4adeb..1554ce9 100644 --- a/src/shared.h +++ b/src/shared.h @@ -131,7 +131,7 @@ void vstr_clear(vstr_t *vstr); int vstr_putc(vstr_t *vstr, char ch); int vstr_cmp(vstr_t *vstr, const char *str); char *vstr_strdup(vstr_t *vstr); -char *vstr_to_cstr(vstr_t *vstr); +const char *vstr_to_cstr(vstr_t *vstr); #define LEXER_BUFFER 1024 |