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 4c36b2a8a7)
This commit is contained in:
Martin Whitaker 2022-03-25 20:34:11 +00:00
parent 5aef565b94
commit 1f4e65ee08
1 changed files with 3 additions and 3 deletions

View File

@ -1,7 +1,7 @@
#ifndef IVL_config_H #ifndef IVL_config_H
#define 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 * This source code is free software; you can redistribute it
* and/or modify it in source code form under the terms of the GNU * 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) static inline char*strndup(const char*s, size_t n)
{ {
if (strlen(s) < n) return strdup(s); if (strlen(s) < n) return strdup(s);
char*tmp = (char*)malloc(n); char*tmp = (char*)malloc(n+1);
strncpy(tmp, s, n); strncpy(tmp, s, n);
tmp[n-1] = 0; tmp[n] = 0;
return tmp; return tmp;
} }
#endif #endif