diff --git a/configure.ac b/configure.ac index 530c073764caa2..76ac9bd6a2ce0a 100644 --- a/configure.ac +++ b/configure.ac @@ -2013,6 +2013,7 @@ AC_REPLACE_FUNCS(strchr) AC_REPLACE_FUNCS(strerror) AC_REPLACE_FUNCS(strlcat) AC_REPLACE_FUNCS(strlcpy) +AC_REPLACE_FUNCS(strndup) AC_REPLACE_FUNCS(strstr) AC_REPLACE_FUNCS(tgamma) diff --git a/include/ruby/missing.h b/include/ruby/missing.h index aea6c9088d9d22..be22b328c77cac 100644 --- a/include/ruby/missing.h +++ b/include/ruby/missing.h @@ -210,6 +210,10 @@ RUBY_EXTERN size_t strlcpy(char *, const char*, size_t); RUBY_EXTERN size_t strlcat(char *, const char*, size_t); #endif +#ifndef HAVE_STRNDUP +RUBY_EXTERN char *strndup(const char *, size_t); +#endif + #ifndef HAVE_FFS RUBY_EXTERN int ffs(int); #endif diff --git a/missing/strndup.c b/missing/strndup.c new file mode 100644 index 00000000000000..0f15e42289517f --- /dev/null +++ b/missing/strndup.c @@ -0,0 +1,39 @@ +/* $OpenBSD: strndup.c,v 1.3 2019/01/25 00:19:25 millert Exp $ */ + +/* + * Copyright (c) 2010 Todd C. Miller + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include + +#include +#include +#include + +char * +strndup(const char *str, size_t maxlen) +{ + char *copy; + size_t len; + + len = strnlen(str, maxlen); + copy = malloc(len + 1); + if (copy != NULL) { + (void)memcpy(copy, str, len); + copy[len] = '\0'; + } + + return copy; +} diff --git a/parse.y b/parse.y index 585130c3465ed6..83e2df9055365d 100644 --- a/parse.y +++ b/parse.y @@ -9315,16 +9315,16 @@ set_number_literal(struct parser_params *p, enum yytokentype type, int suffix, i switch (type) { case tINTEGER: - set_yylval_node(NEW_INTEGER(strdup(tok(p)), base, &_cur_loc)); + set_yylval_node(NEW_INTEGER(strndup(tok(p), toklen(p)), base, &_cur_loc)); break; case tFLOAT: - set_yylval_node(NEW_FLOAT(strdup(tok(p)), &_cur_loc)); + set_yylval_node(NEW_FLOAT(strndup(tok(p), toklen(p)), &_cur_loc)); break; case tRATIONAL: - set_yylval_node(NEW_RATIONAL(strdup(tok(p)), base, seen_point, &_cur_loc)); + set_yylval_node(NEW_RATIONAL(strndup(tok(p), toklen(p)), base, seen_point, &_cur_loc)); break; case tIMAGINARY: - set_yylval_node(NEW_IMAGINARY(strdup(tok(p)), base, seen_point, numeric_type, &_cur_loc)); + set_yylval_node(NEW_IMAGINARY(strndup(tok(p), toklen(p)), base, seen_point, numeric_type, &_cur_loc)); (void)numeric_type; /* for ripper */ break; default: diff --git a/win32/Makefile.sub b/win32/Makefile.sub index 80a43d00dae209..147782ca732b90 100644 --- a/win32/Makefile.sub +++ b/win32/Makefile.sub @@ -315,7 +315,7 @@ LIBS = $(LIBS) gmp.lib LIBS = $(LIBS) imagehlp.lib shlwapi.lib bcrypt.lib $(EXTLIBS) !endif !if !defined(MISSING) -MISSING = crypt.obj ffs.obj langinfo.obj lgamma_r.obj strlcat.obj strlcpy.obj win32/win32.obj win32/file.obj setproctitle.obj +MISSING = crypt.obj ffs.obj langinfo.obj lgamma_r.obj strlcat.obj strlcpy.obj strndup.obj win32/win32.obj win32/file.obj setproctitle.obj !if $(RT_VER) < 120 MISSING = $(MISSING) acosh.obj cbrt.obj erf.obj nan.obj tgamma.obj !endif