From 561638a31f499fa84557d7343c9936820f0b60a3 Mon Sep 17 00:00:00 2001 From: Cary R Date: Sun, 8 Jan 2017 20:44:17 -0800 Subject: [PATCH] Allocate the correct amount of memory when escaping a string --- vpi/sys_priv.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/vpi/sys_priv.c b/vpi/sys_priv.c index 1d5e977bb..b8f333ebe 100644 --- a/vpi/sys_priv.c +++ b/vpi/sys_priv.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003-2016 Stephen Williams (steve@icarus.com) + * Copyright (c) 2003-2017 Stephen Williams (steve@icarus.com) * * This source code is free software; you can redistribute it * and/or modify it in source code form under the terms of the GNU @@ -37,17 +37,17 @@ PLI_UINT64 timerec_to_time64(const struct t_vpi_time*timerec) char *as_escaped(char *arg) { - unsigned idx, cur, cnt, len = strlen(arg); - char *res = (char *) malloc(sizeof(char *) * len); + unsigned idx, cur, cnt, len = strlen(arg) + 1; + char *res = (char *) malloc(sizeof(char) * len); cur = 0; - cnt = len; + cnt = len - 1; for (idx = 0; idx < cnt; idx++) { if (isprint((int)arg[idx])) { res[cur] = arg[idx]; cur += 1; } else { len += 3; - res = (char *) realloc(res, sizeof(char *) * len); + res = (char *) realloc(res, sizeof(char) * len); sprintf(&(res[cur]), "\\%03o", arg[idx]); cur += 4; }