return nullptr instead of out-of-range data
This commit is contained in:
@ -4850,8 +4850,9 @@ int lammps_id_name(void *handle, const char *category, int idx, char *buffer, in
|
|||||||
auto lmp = (LAMMPS *) handle;
|
auto lmp = (LAMMPS *) handle;
|
||||||
|
|
||||||
if (strcmp(category,"compute") == 0) {
|
if (strcmp(category,"compute") == 0) {
|
||||||
if ((idx >=0) && (idx < lmp->modify->ncompute)) {
|
auto icompute = lmp->modify->get_compute_by_index(idx);
|
||||||
strncpy(buffer, lmp->modify->compute[idx]->id, buf_size);
|
if (icompute) {
|
||||||
|
strncpy(buffer, icompute->id, buf_size);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
} else if (strcmp(category,"dump") == 0) {
|
} 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;
|
return 1;
|
||||||
}
|
}
|
||||||
} else if (strcmp(category,"fix") == 0) {
|
} else if (strcmp(category,"fix") == 0) {
|
||||||
if ((idx >=0) && (idx < lmp->modify->nfix)) {
|
auto ifix = lmp->modify->get_fix_by_index(idx);
|
||||||
strncpy(buffer, lmp->modify->fix[idx]->id, buf_size);
|
if (ifix) {
|
||||||
|
strncpy(buffer, ifix->id, buf_size);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
} else if (strcmp(category,"group") == 0) {
|
} else if (strcmp(category,"group") == 0) {
|
||||||
|
|||||||
@ -113,7 +113,9 @@ class Modify : protected Pointers {
|
|||||||
int find_fix(const std::string &);
|
int find_fix(const std::string &);
|
||||||
// new API
|
// new API
|
||||||
Fix *get_fix_by_id(const std::string &) const;
|
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_by_style(const std::string &) const;
|
||||||
const std::vector<Fix *> &get_fix_list();
|
const std::vector<Fix *> &get_fix_list();
|
||||||
|
|
||||||
@ -127,7 +129,9 @@ class Modify : protected Pointers {
|
|||||||
int find_compute(const std::string &);
|
int find_compute(const std::string &);
|
||||||
// new API
|
// new API
|
||||||
Compute *get_compute_by_id(const std::string &) const;
|
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_by_style(const std::string &) const;
|
||||||
const std::vector<Compute *> &get_compute_list();
|
const std::vector<Compute *> &get_compute_list();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user