From 1f4e65ee081aadd34119c6988dc30a27b1fc752c Mon Sep 17 00:00:00 2001 From: Martin Whitaker Date: Fri, 25 Mar 2022 20:34:11 +0000 Subject: [PATCH] vvp: Fix implementation of strndup for Windows (issue #608). The maximum length to copy, n, does not include the terminating null character. (cherry picked from commit 4c36b2a8a7f13bab2d06d857562a195889315f4e) --- vvp/config.h.in | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/vvp/config.h.in b/vvp/config.h.in index 981907ecf..d3dc260c6 100644 --- a/vvp/config.h.in +++ b/vvp/config.h.in @@ -1,7 +1,7 @@ #ifndef IVL_config_H #define IVL_config_H /* - * Copyright (c) 2001-2014 Stephen Williams (steve@icarus.com) + * Copyright (c) 2001-2022 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 @@ -204,9 +204,9 @@ inline int64_t i64round(double x) static inline char*strndup(const char*s, size_t n) { if (strlen(s) < n) return strdup(s); - char*tmp = (char*)malloc(n); + char*tmp = (char*)malloc(n+1); strncpy(tmp, s, n); - tmp[n-1] = 0; + tmp[n] = 0; return tmp; } #endif