Skip to content

Commit

Permalink
libast: strdup(3) maintenance
Browse files Browse the repository at this point in the history
src/lib/libast/string/strdup.c:
- Remove obsolete VMDEBUG preprocessor workaround for vmalloc.
  (re: 40f4665)
- Remove obsolete preprocessor workaround for MSVCRT, as in
  Microsoft Visual C++ Runtime. (re: a28507e, a34e831)
- Use size_t instead of int for the size, fixing the duplication of
  strings larger than 2 GiB. Admittedly a largely theoretical fix,
  but correctness is always preferable.
- De-obfuscate: use legible conditionals and call malloc(3)
  directly instead of via the oldof() macro.
  • Loading branch information
McDutchie committed Dec 9, 2024
1 parent 8883691 commit b456bc9
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions src/lib/libast/string/strdup.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* *
* This software is part of the ast package *
* Copyright (c) 1985-2012 AT&T Intellectual Property *
* Copyright (c) 2020-2023 Contributors to ksh 93u+m *
* Copyright (c) 2020-2024 Contributors to ksh 93u+m *
* and is licensed under the *
* Eclipse Public License, Version 2.0 *
* *
Expand All @@ -18,19 +18,8 @@
* *
***********************************************************************/

#undef VMDEBUG
#define VMDEBUG 0

#if defined(_MSVCRT_H)
#define strdup ______strdup
#endif

#include <ast.h>

#if defined(_MSVCRT_H)
#undef strdup
#endif

/*
* return a copy of s using malloc
*/
Expand All @@ -44,7 +33,14 @@ extern char*
_ast_strdup(const char* s)
{
char* t;
int n;
size_t n;

return (s && (t = oldof(0, char, n = strlen(s) + 1, 0))) ? (char*)memcpy(t, s, n) : NULL;
if (s)
{
n = strlen(s) + 1;
t = malloc(n);
if (t)
return memcpy(t, s, n);
}
return NULL;
}

0 comments on commit b456bc9

Please sign in to comment.