return nullptr instead of out-of-range data

This commit is contained in:
Axel Kohlmeyer
2022-05-05 07:50:45 -04:00
parent 2b1716cb3a
commit 6c28b55c13
2 changed files with 12 additions and 6 deletions

View File

@ -4850,8 +4850,9 @@ int lammps_id_name(void *handle, const char *category, int idx, char *buffer, in
auto lmp = (LAMMPS *) handle;
if (strcmp(category,"compute") == 0) {
if ((idx >=0) && (idx < lmp->modify->ncompute)) {
strncpy(buffer, lmp->modify->compute[idx]->id, buf_size);
auto icompute = lmp->modify->get_compute_by_index(idx);
if (icompute) {
strncpy(buffer, icompute->id, buf_size);
return 1;
}
} else if (strcmp(category,"dump") == 0) {
@ -4860,8 +4861,9 @@ int lammps_id_name(void *handle, const char *category, int idx, char *buffer, in
return 1;
}
} else if (strcmp(category,"fix") == 0) {
if ((idx >=0) && (idx < lmp->modify->nfix)) {
strncpy(buffer, lmp->modify->fix[idx]->id, buf_size);
auto ifix = lmp->modify->get_fix_by_index(idx);
if (ifix) {
strncpy(buffer, ifix->id, buf_size);
return 1;
}
} else if (strcmp(category,"group") == 0) {

View File

@ -113,7 +113,9 @@ class Modify : protected Pointers {
int find_fix(const std::string &);
// new API
Fix *get_fix_by_id(const std::string &) const;
Fix *get_fix_by_index(int idx) const { return fix[idx]; }
Fix *get_fix_by_index(int idx) const {
return ((idx >= 0) && (idx < nfix)) ? fix[idx] : nullptr;
}
const std::vector<Fix *> get_fix_by_style(const std::string &) const;
const std::vector<Fix *> &get_fix_list();
@ -127,7 +129,9 @@ class Modify : protected Pointers {
int find_compute(const std::string &);
// new API
Compute *get_compute_by_id(const std::string &) const;
Compute *get_compute_by_index(int idx) const { return compute[idx]; }
Compute *get_compute_by_index(int idx) const {
return ((idx >= 0) && (idx < nfix)) ? compute[idx] : nullptr;
}
const std::vector<Compute *> get_compute_by_style(const std::string &) const;
const std::vector<Compute *> &get_compute_list();