From 07bc76871cc7fd85db18a1a628b2ca2a83940264 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Sat, 9 Apr 2022 19:01:42 -0400 Subject: [PATCH] avoid unsafe strcpy() --- src/fix_halt.cpp | 11 +++++------ src/fix_print.cpp | 2 +- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/fix_halt.cpp b/src/fix_halt.cpp index 263386e2b0..08713cf6ad 100644 --- a/src/fix_halt.cpp +++ b/src/fix_halt.cpp @@ -107,15 +107,14 @@ FixHalt::FixHalt(LAMMPS *lmp, int narg, char **arg) : } else if (strcmp(arg[iarg],"path") == 0) { if (iarg+2 > narg) error->all(FLERR,"Illegal fix halt command"); ++iarg; - int len = strlen(arg[iarg])+1; delete[] dlimit_path; - dlimit_path = new char[len]; - // strip off quotes, if present + // strip off outer quotes, if present + int len = strlen(arg[iarg])+1; if ( ((arg[iarg][0] == '"') || (arg[iarg][0] == '\'')) && (arg[iarg][0] == arg[iarg][len-2])) { - strcpy(dlimit_path,&arg[iarg][1]); - dlimit_path[len-3] = '\0'; - } else strcpy(dlimit_path,arg[iarg]); + arg[iarg][len-2] = '\0'; + dlimit_path = utils::strdup(arg[iarg]+1); + } else dlimit_path = utils::strdup(arg[iarg]); ++iarg; } else error->all(FLERR,"Illegal fix halt command"); } diff --git a/src/fix_print.cpp b/src/fix_print.cpp index df5b5f77ff..47810c7e48 100644 --- a/src/fix_print.cpp +++ b/src/fix_print.cpp @@ -158,7 +158,7 @@ void FixPrint::end_of_step() modify->clearstep_compute(); - strcpy(copy,text); + strncpy(copy,text,maxcopy); input->substitute(copy,work,maxcopy,maxwork,0); if (var_print) {