use snprintf() instead of sprintf() to avoid buffer overflows when copying style names

This commit is contained in:
Axel Kohlmeyer
2019-06-21 21:23:20 -04:00
parent 7a56a4be24
commit f190647ab4
5 changed files with 26 additions and 26 deletions

View File

@ -455,8 +455,8 @@ void Atom::create_avec(const char *style, int narg, char **arg, int trysuffix)
if (sflag) { if (sflag) {
char estyle[256]; char estyle[256];
if (sflag == 1) sprintf(estyle,"%s/%s",style,lmp->suffix); if (sflag == 1) snprintf(estyle,256,"%s/%s",style,lmp->suffix);
else sprintf(estyle,"%s/%s",style,lmp->suffix2); else snprintf(estyle,256,"%s/%s",style,lmp->suffix2);
int n = strlen(estyle) + 1; int n = strlen(estyle) + 1;
atom_style = new char[n]; atom_style = new char[n];
strcpy(atom_style,estyle); strcpy(atom_style,estyle);
@ -487,7 +487,7 @@ AtomVec *Atom::new_avec(const char *style, int trysuffix, int &sflag)
if (lmp->suffix) { if (lmp->suffix) {
sflag = 1; sflag = 1;
char estyle[256]; char estyle[256];
sprintf(estyle,"%s/%s",style,lmp->suffix); snprintf(estyle,256,"%s/%s",style,lmp->suffix);
if (avec_map->find(estyle) != avec_map->end()) { if (avec_map->find(estyle) != avec_map->end()) {
AtomVecCreator avec_creator = (*avec_map)[estyle]; AtomVecCreator avec_creator = (*avec_map)[estyle];
return avec_creator(lmp); return avec_creator(lmp);
@ -497,7 +497,7 @@ AtomVec *Atom::new_avec(const char *style, int trysuffix, int &sflag)
if (lmp->suffix2) { if (lmp->suffix2) {
sflag = 2; sflag = 2;
char estyle[256]; char estyle[256];
sprintf(estyle,"%s/%s",style,lmp->suffix2); snprintf(estyle,256,"%s/%s",style,lmp->suffix2);
if (avec_map->find(estyle) != avec_map->end()) { if (avec_map->find(estyle) != avec_map->end()) {
AtomVecCreator avec_creator = (*avec_map)[estyle]; AtomVecCreator avec_creator = (*avec_map)[estyle];
return avec_creator(lmp); return avec_creator(lmp);

View File

@ -1734,7 +1734,7 @@ void Domain::add_region(int narg, char **arg)
if (lmp->suffix_enable) { if (lmp->suffix_enable) {
if (lmp->suffix) { if (lmp->suffix) {
char estyle[256]; char estyle[256];
sprintf(estyle,"%s/%s",arg[1],lmp->suffix); snprintf(estyle,256,"%s/%s",arg[1],lmp->suffix);
if (region_map->find(estyle) != region_map->end()) { if (region_map->find(estyle) != region_map->end()) {
RegionCreator region_creator = (*region_map)[estyle]; RegionCreator region_creator = (*region_map)[estyle];
regions[nregion] = region_creator(lmp, narg, arg); regions[nregion] = region_creator(lmp, narg, arg);
@ -1746,7 +1746,7 @@ void Domain::add_region(int narg, char **arg)
if (lmp->suffix2) { if (lmp->suffix2) {
char estyle[256]; char estyle[256];
sprintf(estyle,"%s/%s",arg[1],lmp->suffix2); snprintf(estyle,256,"%s/%s",arg[1],lmp->suffix2);
if (region_map->find(estyle) != region_map->end()) { if (region_map->find(estyle) != region_map->end()) {
RegionCreator region_creator = (*region_map)[estyle]; RegionCreator region_creator = (*region_map)[estyle];
regions[nregion] = region_creator(lmp, narg, arg); regions[nregion] = region_creator(lmp, narg, arg);

View File

@ -238,7 +238,7 @@ Pair *Force::new_pair(const char *style, int trysuffix, int &sflag)
if (lmp->suffix) { if (lmp->suffix) {
sflag = 1; sflag = 1;
char estyle[256]; char estyle[256];
sprintf(estyle,"%s/%s",style,lmp->suffix); snprintf(estyle,256,"%s/%s",style,lmp->suffix);
if (pair_map->find(estyle) != pair_map->end()) { if (pair_map->find(estyle) != pair_map->end()) {
PairCreator pair_creator = (*pair_map)[estyle]; PairCreator pair_creator = (*pair_map)[estyle];
return pair_creator(lmp); return pair_creator(lmp);
@ -247,7 +247,7 @@ Pair *Force::new_pair(const char *style, int trysuffix, int &sflag)
if (lmp->suffix2) { if (lmp->suffix2) {
sflag = 2; sflag = 2;
char estyle[256]; char estyle[256];
sprintf(estyle,"%s/%s",style,lmp->suffix2); snprintf(estyle,256,"%s/%s",style,lmp->suffix2);
if (pair_map->find(estyle) != pair_map->end()) { if (pair_map->find(estyle) != pair_map->end()) {
PairCreator pair_creator = (*pair_map)[estyle]; PairCreator pair_creator = (*pair_map)[estyle];
return pair_creator(lmp); return pair_creator(lmp);
@ -350,7 +350,7 @@ Bond *Force::new_bond(const char *style, int trysuffix, int &sflag)
if (lmp->suffix) { if (lmp->suffix) {
sflag = 1; sflag = 1;
char estyle[256]; char estyle[256];
sprintf(estyle,"%s/%s",style,lmp->suffix); snprintf(estyle,256,"%s/%s",style,lmp->suffix);
if (bond_map->find(estyle) != bond_map->end()) { if (bond_map->find(estyle) != bond_map->end()) {
BondCreator bond_creator = (*bond_map)[estyle]; BondCreator bond_creator = (*bond_map)[estyle];
return bond_creator(lmp); return bond_creator(lmp);
@ -360,7 +360,7 @@ Bond *Force::new_bond(const char *style, int trysuffix, int &sflag)
if (lmp->suffix2) { if (lmp->suffix2) {
sflag = 2; sflag = 2;
char estyle[256]; char estyle[256];
sprintf(estyle,"%s/%s",style,lmp->suffix2); snprintf(estyle,256,"%s/%s",style,lmp->suffix2);
if (bond_map->find(estyle) != bond_map->end()) { if (bond_map->find(estyle) != bond_map->end()) {
BondCreator bond_creator = (*bond_map)[estyle]; BondCreator bond_creator = (*bond_map)[estyle];
return bond_creator(lmp); return bond_creator(lmp);
@ -429,7 +429,7 @@ Angle *Force::new_angle(const char *style, int trysuffix, int &sflag)
if (lmp->suffix) { if (lmp->suffix) {
sflag = 1; sflag = 1;
char estyle[256]; char estyle[256];
sprintf(estyle,"%s/%s",style,lmp->suffix); snprintf(estyle,256,"%s/%s",style,lmp->suffix);
if (angle_map->find(estyle) != angle_map->end()) { if (angle_map->find(estyle) != angle_map->end()) {
AngleCreator angle_creator = (*angle_map)[estyle]; AngleCreator angle_creator = (*angle_map)[estyle];
return angle_creator(lmp); return angle_creator(lmp);
@ -439,7 +439,7 @@ Angle *Force::new_angle(const char *style, int trysuffix, int &sflag)
if (lmp->suffix2) { if (lmp->suffix2) {
sflag = 2; sflag = 2;
char estyle[256]; char estyle[256];
sprintf(estyle,"%s/%s",style,lmp->suffix); snprintf(estyle,256,"%s/%s",style,lmp->suffix);
if (angle_map->find(estyle) != angle_map->end()) { if (angle_map->find(estyle) != angle_map->end()) {
AngleCreator angle_creator = (*angle_map)[estyle]; AngleCreator angle_creator = (*angle_map)[estyle];
return angle_creator(lmp); return angle_creator(lmp);
@ -509,7 +509,7 @@ Dihedral *Force::new_dihedral(const char *style, int trysuffix, int &sflag)
if (lmp->suffix) { if (lmp->suffix) {
sflag = 1; sflag = 1;
char estyle[256]; char estyle[256];
sprintf(estyle,"%s/%s",style,lmp->suffix); snprintf(estyle,256,"%s/%s",style,lmp->suffix);
if (dihedral_map->find(estyle) != dihedral_map->end()) { if (dihedral_map->find(estyle) != dihedral_map->end()) {
DihedralCreator dihedral_creator = (*dihedral_map)[estyle]; DihedralCreator dihedral_creator = (*dihedral_map)[estyle];
return dihedral_creator(lmp); return dihedral_creator(lmp);
@ -519,7 +519,7 @@ Dihedral *Force::new_dihedral(const char *style, int trysuffix, int &sflag)
if (lmp->suffix2) { if (lmp->suffix2) {
sflag = 2; sflag = 2;
char estyle[256]; char estyle[256];
sprintf(estyle,"%s/%s",style,lmp->suffix2); snprintf(estyle,256,"%s/%s",style,lmp->suffix2);
if (dihedral_map->find(estyle) != dihedral_map->end()) { if (dihedral_map->find(estyle) != dihedral_map->end()) {
DihedralCreator dihedral_creator = (*dihedral_map)[estyle]; DihedralCreator dihedral_creator = (*dihedral_map)[estyle];
return dihedral_creator(lmp); return dihedral_creator(lmp);
@ -588,7 +588,7 @@ Improper *Force::new_improper(const char *style, int trysuffix, int &sflag)
if (lmp->suffix) { if (lmp->suffix) {
sflag = 1; sflag = 1;
char estyle[256]; char estyle[256];
sprintf(estyle,"%s/%s",style,lmp->suffix); snprintf(estyle,256,"%s/%s",style,lmp->suffix);
if (improper_map->find(estyle) != improper_map->end()) { if (improper_map->find(estyle) != improper_map->end()) {
ImproperCreator improper_creator = (*improper_map)[estyle]; ImproperCreator improper_creator = (*improper_map)[estyle];
return improper_creator(lmp); return improper_creator(lmp);
@ -598,7 +598,7 @@ Improper *Force::new_improper(const char *style, int trysuffix, int &sflag)
if (lmp->suffix2) { if (lmp->suffix2) {
sflag = 2; sflag = 2;
char estyle[256]; char estyle[256];
sprintf(estyle,"%s/%s",style,lmp->suffix2); snprintf(estyle,256,"%s/%s",style,lmp->suffix2);
if (improper_map->find(estyle) != improper_map->end()) { if (improper_map->find(estyle) != improper_map->end()) {
ImproperCreator improper_creator = (*improper_map)[estyle]; ImproperCreator improper_creator = (*improper_map)[estyle];
return improper_creator(lmp); return improper_creator(lmp);
@ -671,7 +671,7 @@ KSpace *Force::new_kspace(const char *style, int trysuffix, int &sflag)
if (lmp->suffix) { if (lmp->suffix) {
sflag = 1; sflag = 1;
char estyle[256]; char estyle[256];
sprintf(estyle,"%s/%s",style,lmp->suffix); snprintf(estyle,256,"%s/%s",style,lmp->suffix);
if (kspace_map->find(estyle) != kspace_map->end()) { if (kspace_map->find(estyle) != kspace_map->end()) {
KSpaceCreator kspace_creator = (*kspace_map)[estyle]; KSpaceCreator kspace_creator = (*kspace_map)[estyle];
return kspace_creator(lmp); return kspace_creator(lmp);
@ -681,7 +681,7 @@ KSpace *Force::new_kspace(const char *style, int trysuffix, int &sflag)
if (lmp->suffix2) { if (lmp->suffix2) {
sflag = 1; sflag = 1;
char estyle[256]; char estyle[256];
sprintf(estyle,"%s/%s",style,lmp->suffix2); snprintf(estyle,256,"%s/%s",style,lmp->suffix2);
if (kspace_map->find(estyle) != kspace_map->end()) { if (kspace_map->find(estyle) != kspace_map->end()) {
KSpaceCreator kspace_creator = (*kspace_map)[estyle]; KSpaceCreator kspace_creator = (*kspace_map)[estyle];
return kspace_creator(lmp); return kspace_creator(lmp);
@ -735,8 +735,8 @@ void Force::store_style(char *&str, const char *style, int sflag)
{ {
if (sflag) { if (sflag) {
char estyle[256]; char estyle[256];
if (sflag == 1) sprintf(estyle,"%s/%s",style,lmp->suffix); if (sflag == 1) snprintf(estyle,256,"%s/%s",style,lmp->suffix);
else sprintf(estyle,"%s/%s",style,lmp->suffix2); else snprintf(estyle,256,"%s/%s",style,lmp->suffix2);
int n = strlen(estyle) + 1; int n = strlen(estyle) + 1;
str = new char[n]; str = new char[n];
strcpy(str,estyle); strcpy(str,estyle);

View File

@ -1818,11 +1818,11 @@ void Input::pair_style()
if (!match && lmp->suffix_enable) { if (!match && lmp->suffix_enable) {
char estyle[256]; char estyle[256];
if (lmp->suffix) { if (lmp->suffix) {
sprintf(estyle,"%s/%s",arg[0],lmp->suffix); snprintf(estyle,256,"%s/%s",arg[0],lmp->suffix);
if (strcmp(estyle,force->pair_style) == 0) match = 1; if (strcmp(estyle,force->pair_style) == 0) match = 1;
} }
if (lmp->suffix2) { if (lmp->suffix2) {
sprintf(estyle,"%s/%s",arg[0],lmp->suffix2); snprintf(estyle,256,"%s/%s",arg[0],lmp->suffix2);
if (strcmp(estyle,force->pair_style) == 0) match = 1; if (strcmp(estyle,force->pair_style) == 0) match = 1;
} }
} }

View File

@ -316,8 +316,8 @@ void Update::create_integrate(int narg, char **arg, int trysuffix)
if (sflag) { if (sflag) {
char estyle[256]; char estyle[256];
if (sflag == 1) sprintf(estyle,"%s/%s",arg[0],lmp->suffix); if (sflag == 1) snprintf(estyle,256,"%s/%s",arg[0],lmp->suffix);
else sprintf(estyle,"%s/%s",arg[0],lmp->suffix2); else snprintf(estyle,256,"%s/%s",arg[0],lmp->suffix2);
int n = strlen(estyle) + 1; int n = strlen(estyle) + 1;
integrate_style = new char[n]; integrate_style = new char[n];
strcpy(integrate_style,estyle); strcpy(integrate_style,estyle);
@ -339,7 +339,7 @@ void Update::new_integrate(char *style, int narg, char **arg,
if (lmp->suffix) { if (lmp->suffix) {
sflag = 1; sflag = 1;
char estyle[256]; char estyle[256];
sprintf(estyle,"%s/%s",style,lmp->suffix); snprintf(estyle,256,"%s/%s",style,lmp->suffix);
if (integrate_map->find(estyle) != integrate_map->end()) { if (integrate_map->find(estyle) != integrate_map->end()) {
IntegrateCreator integrate_creator = (*integrate_map)[estyle]; IntegrateCreator integrate_creator = (*integrate_map)[estyle];
integrate = integrate_creator(lmp, narg, arg); integrate = integrate_creator(lmp, narg, arg);
@ -350,7 +350,7 @@ void Update::new_integrate(char *style, int narg, char **arg,
if (lmp->suffix2) { if (lmp->suffix2) {
sflag = 2; sflag = 2;
char estyle[256]; char estyle[256];
sprintf(estyle,"%s/%s",style,lmp->suffix2); snprintf(estyle,256,"%s/%s",style,lmp->suffix2);
if (integrate_map->find(estyle) != integrate_map->end()) { if (integrate_map->find(estyle) != integrate_map->end()) {
IntegrateCreator integrate_creator = (*integrate_map)[estyle]; IntegrateCreator integrate_creator = (*integrate_map)[estyle];
integrate = integrate_creator(lmp, narg, arg); integrate = integrate_creator(lmp, narg, arg);