Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Bug #20398] Fix heap-buffer-overflow read in set_number_literal #10393

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 4 additions & 0 deletions include/ruby/missing.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 39 additions & 0 deletions missing/strndup.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* $OpenBSD: strndup.c,v 1.3 2019/01/25 00:19:25 millert Exp $ */

/*
* Copyright (c) 2010 Todd C. Miller <[email protected]>
*
* 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 <sys/types.h>

#include <stddef.h>
#include <stdlib.h>
#include <string.h>

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;
}
8 changes: 4 additions & 4 deletions parse.y
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion win32/Makefile.sub
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down