From c9fa1d4350076f923597317f8ee7d86ce8d17fec Mon Sep 17 00:00:00 2001 From: Darth Vader Date: Sun, 15 Jan 2023 06:02:06 +0000 Subject: [PATCH 1/7] Squashed 'examples/c/' changes from 2a2707ae..11a83991 11a83991 [iphreeqc] Issue 8 -- change sprintf to snprintf for CRAN (#9) git-subtree-dir: examples/c git-subtree-split: 11a83991492940a25d12e4ffe94623f2b6b05853 --- advect/advect.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/advect/advect.c b/advect/advect.c index b7534c62..ef924a41 100644 --- a/advect/advect.c +++ b/advect/advect.c @@ -23,7 +23,7 @@ void ExtractWrite(int cell) switch (vt[j]) { case TT_DOUBLE: dv[j] = v.dVal; - sprintf(sv[j], "%23.15e", v.dVal); + snprintf(sv[j], sizeof(sv[j]), "%23.15e", v.dVal); break; case TT_STRING: strcpy(sv[j], v.sVal); From eab05a830c0d10ca0eed06b7e4b917986b7b5e93 Mon Sep 17 00:00:00 2001 From: Darth Vader Date: Sun, 15 Jan 2023 06:02:07 +0000 Subject: [PATCH 2/7] Squashed 'src/' changes from f676db22..b32560ef b32560ef [iphreeqc] Issue 8 -- change sprintf to snprintf for CRAN (#9) git-subtree-dir: src git-subtree-split: b32560efa0718ceb1afc7d5b2abf43011d0063c9 --- IPhreeqc.cpp | 10 +-- IPhreeqc_interface_F.cpp | 4 +- fwrap.cpp | 4 +- phreeqcpp/PBasic.cpp | 39 +++++----- phreeqcpp/PBasic.h | 1 + phreeqcpp/PHRQ_io_output.cpp | 4 +- phreeqcpp/Phreeqc.h | 2 +- phreeqcpp/ReadClass.cxx | 6 +- phreeqcpp/basicsubs.cpp | 3 +- phreeqcpp/class_main.cpp | 11 ++- phreeqcpp/inverse.cpp | 60 +++++++-------- phreeqcpp/kinetics.cpp | 2 +- phreeqcpp/mainsubs.cpp | 22 +++--- phreeqcpp/parse.cpp | 4 +- phreeqcpp/pitzer.cpp | 4 +- phreeqcpp/print.cpp | 6 +- phreeqcpp/readtr.cpp | 140 +++++++++++++++++------------------ phreeqcpp/transport.cpp | 40 +++++----- phreeqcpp/utilities.cpp | 22 +++--- 19 files changed, 196 insertions(+), 188 deletions(-) diff --git a/IPhreeqc.cpp b/IPhreeqc.cpp index d5f4640c..e648b313 100644 --- a/IPhreeqc.cpp +++ b/IPhreeqc.cpp @@ -485,7 +485,7 @@ VRESULT IPhreeqc::GetSelectedOutputValue(int row, int col, VAR* pVAR) { char buffer[120]; v = VR_INVALIDARG; - ::sprintf(buffer, "GetSelectedOutputValue: VR_INVALIDARG Invalid selected-output user number %d.\n", this->CurrentSelectedOutputUserNumber); + ::snprintf(buffer, sizeof(buffer), "GetSelectedOutputValue: VR_INVALIDARG Invalid selected-output user number %d.\n", this->CurrentSelectedOutputUserNumber); this->AddError(buffer); this->update_errors(); } @@ -512,13 +512,13 @@ VRESULT IPhreeqc::GetSelectedOutputValue2(int row, int col, int *vtype, double* case TT_LONG: *vtype = TT_DOUBLE; *dvalue = (double)v.lVal; - ::sprintf(buffer, "%ld", v.lVal); + ::snprintf(buffer, sizeof(buffer), "%ld", v.lVal); ::strncpy(svalue, buffer, svalue_length); break; case TT_DOUBLE: *vtype = v.type; *dvalue = v.dVal; - ::sprintf(buffer, "%23.15e", v.dVal); + ::snprintf(buffer, sizeof(buffer), "%23.15e", v.dVal); ::strncpy(svalue, buffer, svalue_length); break; case TT_STRING: @@ -1238,7 +1238,7 @@ void IPhreeqc::do_run(const char* sz_routine, std::istream* pis, PFN_PRERUN_CALL #ifdef PHREEQ98 AddSeries = !connect_simulations; #endif - ::sprintf(token, "Reading input data for simulation %d.", this->PhreeqcPtr->simulation); + ::snprintf(token, sizeof(token), "Reading input data for simulation %d.", this->PhreeqcPtr->simulation); // bool save_punch_in = this->PhreeqcPtr->SelectedOutput_map.size() > 0; @@ -1270,7 +1270,7 @@ void IPhreeqc::do_run(const char* sz_routine, std::istream* pis, PFN_PRERUN_CALL ASSERT(this->PhreeqcPtr->SelectedOutput_map.size() == this->SelectedOutputStringMap.size()); if (!this->PhreeqcPtr->title_x.empty()) { - ::sprintf(token, "TITLE"); + ::snprintf(token, sizeof(token), "TITLE"); this->PhreeqcPtr->dup_print(token, TRUE); if (this->PhreeqcPtr->pr.headings == TRUE) { diff --git a/IPhreeqc_interface_F.cpp b/IPhreeqc_interface_F.cpp index ab1125de..80c2ed14 100644 --- a/IPhreeqc_interface_F.cpp +++ b/IPhreeqc_interface_F.cpp @@ -318,13 +318,13 @@ GetSelectedOutputValueF(int *id, int *row, int *col, int *vtype, double* dvalue, case TT_LONG: *vtype = TT_DOUBLE; *dvalue = (double)v.lVal; - ::sprintf(buffer, "%ld", v.lVal); + ::snprintf(buffer, sizeof(buffer), "%ld", v.lVal); padfstring(svalue, buffer, svalue_length); break; case TT_DOUBLE: *vtype = v.type; *dvalue = v.dVal; - ::sprintf(buffer, "%23.15e", v.dVal); + ::snprintf(buffer, sizeof(buffer), "%23.15e", v.dVal); padfstring(svalue, buffer, svalue_length); break; case TT_STRING: diff --git a/fwrap.cpp b/fwrap.cpp index 2c096f93..bdec8e5a 100644 --- a/fwrap.cpp +++ b/fwrap.cpp @@ -347,13 +347,13 @@ GetSelectedOutputValueF(int *id, int *row, int *col, int *vtype, double* dvalue, case TT_LONG: *vtype = TT_DOUBLE; *dvalue = (double)v.lVal; - ::sprintf(buffer, "%ld", v.lVal); + ::snprintf(buffer, sizeof(buffer), "%ld", v.lVal); padfstring(svalue, buffer, (unsigned int) svalue_length); break; case TT_DOUBLE: *vtype = v.type; *dvalue = v.dVal; - ::sprintf(buffer, "%23.15e", v.dVal); + ::snprintf(buffer, sizeof(buffer), "%23.15e", v.dVal); padfstring(svalue, buffer, (unsigned int) svalue_length); break; case TT_STRING: diff --git a/phreeqcpp/PBasic.cpp b/phreeqcpp/PBasic.cpp index 09016cd9..7233f91c 100644 --- a/phreeqcpp/PBasic.cpp +++ b/phreeqcpp/PBasic.cpp @@ -499,22 +499,22 @@ numtostr(char * Result, LDBLE n) //if (PhreeqcPtr->current_selected_output != NULL && // !PhreeqcPtr->current_selected_output->Get_high_precision()) //{ - // sprintf(l_s, "%12.0f", (double) n); + // snprintf(l_s, PhreeqcPtr->max_line, "%12.0f", (double) n); //} //else //{ - // sprintf(l_s, "%20.0f", (double) n); + // snprintf(l_s, PhreeqcPtr->max_line, "%20.0f", (double) n); //} bool temp_high_precision = (PhreeqcPtr->current_selected_output != NULL) ? PhreeqcPtr->current_selected_output->Get_high_precision() : PhreeqcPtr->high_precision; if (!temp_high_precision) { - sprintf(l_s, "%12.0f", (double) n); + snprintf(l_s, PhreeqcPtr->max_line, "%12.0f", (double) n); } else { - sprintf(l_s, "%20.0f", (double) n); + snprintf(l_s, PhreeqcPtr->max_line, "%20.0f", (double) n); } } else @@ -524,11 +524,11 @@ numtostr(char * Result, LDBLE n) PhreeqcPtr->high_precision; if (!temp_high_precision) { - sprintf(l_s, "%12.4e", (double) n); + snprintf(l_s, PhreeqcPtr->max_line, "%12.4e", (double) n); } else { - sprintf(l_s, "%20.12e", (double) n); + snprintf(l_s, PhreeqcPtr->max_line, "%20.12e", (double) n); } } i = (int) strlen(l_s) + 1; @@ -539,8 +539,8 @@ numtostr(char * Result, LDBLE n) PhreeqcPtr->free_check_null(l_s); return (Result); /* } else { - if (PhreeqcPtr->punch.high_precision == FALSE) sprintf(l_s, "%30.10f", n); - else sprintf(l_s, "%30.12f", n); + if (PhreeqcPtr->punch.high_precision == FALSE) snprintf(l_s, PhreeqcPtr->max_line, "%30.10f", n); + else snprintf(l_s, PhreeqcPtr->max_line, "%30.12f", n); i = strlen(l_s) + 1; do { i--; @@ -612,6 +612,7 @@ parse(char * l_inbuf, tokenrec ** l_buf) if (j + 1 > m) m = j + 1; t->UU.sp = (char *) PhreeqcPtr->PHRQ_calloc(m, sizeof(char)); + t->sp_sz = m; if (t->UU.sp == NULL) { PhreeqcPtr->malloc_error(); @@ -739,6 +740,7 @@ parse(char * l_inbuf, tokenrec ** l_buf) if (m < 256) m = 256; t->UU.sp = (char *) PhreeqcPtr->PHRQ_calloc(m, sizeof(char)); + t->sp_sz = m; if (t->UU.sp == NULL) { PhreeqcPtr->malloc_error(); @@ -746,7 +748,7 @@ parse(char * l_inbuf, tokenrec ** l_buf) exit(4); #endif } - sprintf(t->UU.sp, "%.*s", + snprintf(t->UU.sp, t->sp_sz, "%.*s", (int) (strlen(l_inbuf) - i + 1), l_inbuf + i - 1); i = (int) strlen(l_inbuf) + 1; @@ -2232,7 +2234,7 @@ factor(struct LOC_exec * LINK) { if (PhreeqcPtr->use.Get_mix_in()) { - sprintf(string, "Mix %d", PhreeqcPtr->use.Get_n_mix_user()); + snprintf(string, sizeof(string), "Mix %d", PhreeqcPtr->use.Get_n_mix_user()); n.UU.sval = PhreeqcPtr->string_duplicate(string); } else @@ -2251,7 +2253,7 @@ factor(struct LOC_exec * LINK) } else if (PhreeqcPtr->state == ADVECTION || PhreeqcPtr->state == TRANSPORT || PhreeqcPtr->state == PHAST) { - sprintf(string, "Cell %d", PhreeqcPtr->cell_no); + snprintf(string, sizeof(string), "Cell %d", PhreeqcPtr->cell_no); n.UU.sval = PhreeqcPtr->string_duplicate(string); } else @@ -3608,7 +3610,7 @@ factor(struct LOC_exec * LINK) std::string std_num; { - sprintf(token, "%*.*e", length, width, nmbr); + snprintf(token, sizeof(token), "%*.*e", length, width, nmbr); std_num = token; } @@ -3651,7 +3653,7 @@ factor(struct LOC_exec * LINK) std::string std_num; { - sprintf(token, "%*.*f", length, width, nmbr); + snprintf(token, sizeof(token), "%*.*f", length, width, nmbr); std_num = token; } @@ -4729,12 +4731,12 @@ cmdload(bool merging, char * name, struct LOC_exec *LINK) cmdnew(LINK); if (f != NULL) { - sprintf(STR1, "%s.TEXT", name); + snprintf(STR1, sizeof(STR1), "%s.TEXT", name); f = freopen(STR1, "r", f); } else { - sprintf(STR1, "%s.TEXT", name); + snprintf(STR1, sizeof(STR1), "%s.TEXT", name); f = fopen(STR1, "r"); } if (f == NULL) @@ -7213,6 +7215,7 @@ _NilCheck(void) return _Escape(-3); } +#ifdef SKIP /* The following is suitable for the HP Pascal operating system. It might want to be revised when emulating another system. */ @@ -7233,7 +7236,7 @@ _ShowEscape(char *buf, int code, int ior, char *prefix) } if (code == -10) { - sprintf(bufp, "Pascal system I/O error %d", ior); + snprintf(bufp, sizeof(bufp), "Pascal system I/O error %d", ior); // FIXME -- replace sizeof switch (ior) { case 3: @@ -7273,7 +7276,7 @@ _ShowEscape(char *buf, int code, int ior, char *prefix) } else { - sprintf(bufp, "Pascal system error %d", code); + snprintf(bufp, sizeof(bufp), "Pascal system error %d", code); // FIXME -- replace sizeof switch (code) { case -2: @@ -7307,7 +7310,7 @@ _ShowEscape(char *buf, int code, int ior, char *prefix) } return buf; } - +#endif int PBasic:: _Escape(int code) { diff --git a/phreeqcpp/PBasic.h b/phreeqcpp/PBasic.h index 749b7077..95d77db2 100644 --- a/phreeqcpp/PBasic.h +++ b/phreeqcpp/PBasic.h @@ -79,6 +79,7 @@ typedef struct tokenrec //#ifdef PHREEQCI_GUI size_t n_sz; char *sz_num; + size_t sp_sz; //#endif } tokenrec; diff --git a/phreeqcpp/PHRQ_io_output.cpp b/phreeqcpp/PHRQ_io_output.cpp index 5eaeb5c1..8cc33f05 100644 --- a/phreeqcpp/PHRQ_io_output.cpp +++ b/phreeqcpp/PHRQ_io_output.cpp @@ -136,7 +136,7 @@ fpunchf_user(int user_index, const char *format, double d) warning_msg(error_string); fpunchf_user_s_warning = 1; } - sprintf(fpunchf_user_buffer, "no_heading_%d", + snprintf(fpunchf_user_buffer, sizeof(fpunchf_user_buffer), "no_heading_%d", (user_index - user_punch_count_headings) + 1); name = fpunchf_user_buffer; } @@ -173,7 +173,7 @@ fpunchf_user(int user_index, const char *format, char * d) warning_msg(error_string); fpunchf_user_s_warning = 1; } - sprintf(fpunchf_user_buffer, "no_heading_%d", + snprintf(fpunchf_user_buffer, sizeof(fpunchf_user_buffer), "no_heading_%d", (user_index - user_punch_count_headings) + 1); name = fpunchf_user_buffer; } diff --git a/phreeqcpp/Phreeqc.h b/phreeqcpp/Phreeqc.h index dd414fb2..c3f20aae 100644 --- a/phreeqcpp/Phreeqc.h +++ b/phreeqcpp/Phreeqc.h @@ -481,7 +481,7 @@ public: // parse.cpp ------------------------------- int check_eqn(int association); - int get_charge(char* charge, LDBLE* z); + int get_charge(char* charge, size_t charge_size, LDBLE* z); int get_elt(const char** t_ptr, std::string& element, int* i); int get_elts_in_species(const char** t_ptr, LDBLE coef); int get_num(const char** t_ptr, LDBLE* num); diff --git a/phreeqcpp/ReadClass.cxx b/phreeqcpp/ReadClass.cxx index 937d6575..fda93428 100644 --- a/phreeqcpp/ReadClass.cxx +++ b/phreeqcpp/ReadClass.cxx @@ -551,7 +551,7 @@ run_as_cells(void) rate_sim_time = 0; for (reaction_step = 1; reaction_step <= count_steps; reaction_step++) { - sprintf(token, "Reaction step %d.", reaction_step); + snprintf(token, sizeof(token), "Reaction step %d.", reaction_step); if (reaction_step > 1 && incremental_reactions == FALSE) { copy_use(-2); @@ -635,7 +635,7 @@ run_as_cells(void) rate_sim_time = 0; reaction_step = 1; - sprintf(token, "Reaction step %d.", reaction_step); + snprintf(token, sizeof(token), "Reaction step %d.", reaction_step); dup_print(token, FALSE); /* @@ -763,7 +763,7 @@ run_as_cells(void) rate_sim_time = 0; for (reaction_step = 1; reaction_step <= count_steps; reaction_step++) { - sprintf(token, "Reaction step %d.", reaction_step); + snprintf(token, sizeof(token), "Reaction step %d.", reaction_step); if (reaction_step > 1 && incremental_reactions == FALSE) { copy_use(-2); diff --git a/phreeqcpp/basicsubs.cpp b/phreeqcpp/basicsubs.cpp index a21f34d5..3e93470b 100644 --- a/phreeqcpp/basicsubs.cpp +++ b/phreeqcpp/basicsubs.cpp @@ -2097,12 +2097,13 @@ match_elts_in_species(const char *name, const char *mytemplate) * write out string */ token[0] = '\0'; + assert(MAX_LENGTH == sizeof(token1)); for (i = 0; i < count_match_tokens; i++) { strcat(token, match_vector[i].first.c_str()); if (match_vector[i].second != 1.0) { - sprintf(token1, "%g", (double) match_vector[i].second); + snprintf(token1, sizeof(token1), "%g", (double) match_vector[i].second); strcat(token, token1); } } diff --git a/phreeqcpp/class_main.cpp b/phreeqcpp/class_main.cpp index 74721e86..fe3d0e77 100644 --- a/phreeqcpp/class_main.cpp +++ b/phreeqcpp/class_main.cpp @@ -1,3 +1,6 @@ +#ifdef DOS +#include +#endif #include "Phreeqc.h" #include "NameDouble.h" @@ -296,9 +299,9 @@ write_banner(void) /* version */ #ifdef NPP - len = sprintf(buffer, "* PHREEQC-%s *", "3.7.1"); + len = snprintf(buffer, sizeof(buffer), "* PHREEQC-%s *", "3.7.1"); #else - len = sprintf(buffer, "* PHREEQC-%s *", "@VERSION@"); + len = snprintf(buffer, sizeof(buffer), "* PHREEQC-%s *", "@VERSION@"); #endif indent = (44 - len) / 2; screen_msg(sformatf("%14c║%*c%s%*c║\n", ' ', indent, ' ', buffer, @@ -320,9 +323,9 @@ write_banner(void) /* date */ #ifdef NPP - len = sprintf(buffer, "%s", "July 5, 2021"); + len = snprintf(buffer, sizeof(buffer), "%s", "July 5, 2021"); #else - len = sprintf(buffer, "%s", "@VER_DATE@"); + len = snprintf(buffer, sizeof(buffer), "%s", "@VER_DATE@"); #endif indent = (44 - len) / 2; screen_msg(sformatf("%14c║%*c%s%*c║\n", ' ', indent, ' ', buffer, diff --git a/phreeqcpp/inverse.cpp b/phreeqcpp/inverse.cpp index 81a33781..b9bbcd0d 100644 --- a/phreeqcpp/inverse.cpp +++ b/phreeqcpp/inverse.cpp @@ -371,7 +371,7 @@ setup_inverse(class inverse *inv_ptr) f = -1.0; } column = i; - sprintf(token, "soln %d", i); + snprintf(token, sizeof(token), "soln %d", i); col_name[column] = string_hsave(token); for (j = 0; j < (int)master.size(); j++) { @@ -538,7 +538,7 @@ setup_inverse(class inverse *inv_ptr) { my_array[(size_t)row * max_column_count + (size_t)column] = 0.0; } - sprintf(token, "%s %d", row_name[row], j); + snprintf(token, sizeof(token), "%s %d", row_name[row], j); col_name[column] = string_hsave(token); column++; } @@ -549,13 +549,13 @@ setup_inverse(class inverse *inv_ptr) for (i = 0; i < inv_ptr->count_solns; i++) { - sprintf(token, "ph %d", i); + snprintf(token, sizeof(token), "ph %d", i); col_name[column] = string_hsave(token); column++; } /* put names in col_name for water */ - sprintf(token, "water"); + snprintf(token, sizeof(token), "water"); col_name[column] = string_hsave(token); column++; @@ -564,7 +564,7 @@ setup_inverse(class inverse *inv_ptr) { for (j = 0; j < inv_ptr->isotope_unknowns.size(); j++) { - sprintf(token, "%d%s %d", + snprintf(token, sizeof(token), "%d%s %d", (int) inv_ptr->isotope_unknowns[j].isotope_number, inv_ptr->isotope_unknowns[j].elt_name, i); col_name[column] = string_hsave(token); @@ -581,7 +581,7 @@ setup_inverse(class inverse *inv_ptr) { for (j = 0; j < inv_ptr->isotopes.size(); j++) { - sprintf(token, "%d%s %s", + snprintf(token, sizeof(token), "%d%s %s", (int) inv_ptr->isotopes[j].isotope_number, inv_ptr->isotopes[j].elt_name, inv_ptr->phases[i].phase->name); @@ -649,7 +649,7 @@ setup_inverse(class inverse *inv_ptr) my_array[count_rows * max_column_count + (size_t)column] = 0.0; } } - sprintf(token, "%s %d", "charge", i); + snprintf(token, sizeof(token), "%s %d", "charge", i); row_name[count_rows] = string_hsave(token); count_rows++; } @@ -672,7 +672,7 @@ setup_inverse(class inverse *inv_ptr) my_array[count_rows * max_column_count + (size_t)column] = inv_ptr->dalk_dc[i]; } - sprintf(token, "%s %d", "dAlk", i); + snprintf(token, sizeof(token), "%s %d", "dAlk", i); row_name[count_rows] = string_hsave(token); count_rows++; } @@ -688,7 +688,7 @@ setup_inverse(class inverse *inv_ptr) for (size_t j = 0; j < inv_ptr->isotopes.size(); j++) { isotope_balance_equation(inv_ptr, (int)count_rows, (int)j); - sprintf(token, "%d%s", (int) inv_ptr->isotopes[j].isotope_number, + snprintf(token, sizeof(token), "%d%s", (int) inv_ptr->isotopes[j].isotope_number, inv_ptr->isotopes[j].elt_name); row_name[count_rows] = string_hsave(token); count_rows++; @@ -761,7 +761,7 @@ setup_inverse(class inverse *inv_ptr) } my_array[count_rows * max_column_count + (size_t)column] = 1.0 * f; my_array[count_rows * max_column_count + (size_t)i] = -coef * f; - sprintf(token, "%s %s", inv_ptr->elts[j].master->elt->name, "eps+"); + snprintf(token, sizeof(token), "%s %s", inv_ptr->elts[j].master->elt->name, "eps+"); row_name[count_rows] = string_hsave(token); count_rows++; @@ -795,7 +795,7 @@ setup_inverse(class inverse *inv_ptr) my_array[count_rows * max_column_count + (size_t)i] = -coef * f; my_array[count_rows * max_column_count + (size_t)column] = -1.0 * f; - sprintf(token, "%s %s", inv_ptr->elts[j].master->elt->name, + snprintf(token, sizeof(token), "%s %s", inv_ptr->elts[j].master->elt->name, "eps-"); row_name[count_rows] = string_hsave(token); count_rows++; @@ -821,7 +821,7 @@ setup_inverse(class inverse *inv_ptr) my_array[count_rows * max_column_count + (size_t)column] = 1.0; my_array[count_rows * max_column_count + (size_t)i] = -coef; - sprintf(token, "%s %s", "pH", "eps+"); + snprintf(token, sizeof(token), "%s %s", "pH", "eps+"); row_name[count_rows] = string_hsave(token); count_rows++; @@ -829,7 +829,7 @@ setup_inverse(class inverse *inv_ptr) my_array[count_rows * max_column_count + (size_t)column] = -1.0; my_array[count_rows * max_column_count + (size_t)i] = -coef; - sprintf(token, "%s %s", "pH", "eps-"); + snprintf(token, sizeof(token), "%s %s", "pH", "eps-"); row_name[count_rows] = string_hsave(token); count_rows++; } @@ -845,7 +845,7 @@ setup_inverse(class inverse *inv_ptr) /* set upper limit of change in positive direction */ my_array[count_rows * max_column_count + (size_t)column] = 1.0; my_array[count_rows * max_column_count + count_unknowns] = coef; - sprintf(token, "%s %s", "water", "eps+"); + snprintf(token, sizeof(token), "%s %s", "water", "eps+"); row_name[count_rows] = string_hsave(token); count_rows++; @@ -853,7 +853,7 @@ setup_inverse(class inverse *inv_ptr) my_array[count_rows * max_column_count + (size_t)column] = -1.0; my_array[count_rows * max_column_count + count_unknowns] = coef; - sprintf(token, "%s %s", "water", "eps-"); + snprintf(token, sizeof(token), "%s %s", "water", "eps-"); row_name[count_rows] = string_hsave(token); count_rows++; } @@ -890,7 +890,7 @@ setup_inverse(class inverse *inv_ptr) /* set upper limit of change in positive direction */ my_array[count_rows * max_column_count + (size_t)column] = 1.0; my_array[count_rows * max_column_count + (size_t)i] = -coef; - sprintf(token, "%d%s %s", + snprintf(token, sizeof(token), "%d%s %s", (int) kit->second.Get_isotope_number(), kit->second.Get_elt_name().c_str(), "eps+"); row_name[count_rows] = string_hsave(token); @@ -900,7 +900,7 @@ setup_inverse(class inverse *inv_ptr) my_array[count_rows * max_column_count + (size_t)column] = -1.0; my_array[count_rows * max_column_count + (size_t)i] = -coef; - sprintf(token, "%d%s %s", + snprintf(token, sizeof(token), "%d%s %s", (int) kit->second.Get_isotope_number(), kit->second.Get_elt_name().c_str(), "eps-"); row_name[count_rows] = string_hsave(token); @@ -1777,7 +1777,7 @@ print_model(class inverse *inv_ptr) d2 = 0.0; if (equal(d3, 0.0, MIN_TOTAL_INVERSE) == TRUE) d3 = 0.0; - sprintf(token, "%d%s", + snprintf(token, sizeof(token), "%d%s", (int) inv_ptr->isotope_unknowns[j]. isotope_number, inv_ptr->isotope_unknowns[j].elt_name); @@ -1853,7 +1853,7 @@ print_model(class inverse *inv_ptr) d2 = 0.0; if (equal(d3, 0.0, 1e-7) == TRUE) d3 = 0.0; - sprintf(token, "%d%s %s", + snprintf(token, sizeof(token), "%d%s %s", (int) inv_ptr->isotopes[j].isotope_number, inv_ptr->isotopes[j].elt_name, inv_ptr->phases[i].phase->name); @@ -2060,7 +2060,7 @@ punch_model_heading(class inverse *inv_ptr) */ for (i = 0; i < inv_ptr->count_solns; i++) { - sprintf(token, "Soln_%d", inv_ptr->solns[i]); + snprintf(token, sizeof(token), "Soln_%d", inv_ptr->solns[i]); std::string tok1(token); tok1.append("_min"); std::string tok2(token); @@ -3618,7 +3618,7 @@ check_isotopes(class inverse *inv_ptr) } else { - sprintf(token, "%g%s", + snprintf(token, sizeof(token), "%g%s", (double) kit->second.Get_isotope_number(), kit->second.Get_elt_name().c_str()); for (l = 0; l < count_iso_defaults; l++) @@ -3764,7 +3764,7 @@ phase_isotope_inequalities(class inverse *inv_ptr) my_array[count_rows * max_column_count + (size_t)col_phases + (size_t)i] = inv_ptr->phases[i].isotopes[j].ratio_uncertainty; my_array[count_rows * max_column_count + (size_t)column] = 1.0; - sprintf(token, "%s %s", inv_ptr->phases[i].phase->name, + snprintf(token, sizeof(token), "%s %s", inv_ptr->phases[i].phase->name, "iso pos"); row_name[count_rows] = string_hsave(token); count_rows++; @@ -3772,7 +3772,7 @@ phase_isotope_inequalities(class inverse *inv_ptr) my_array[count_rows * max_column_count + (size_t)col_phases + (size_t)i] = inv_ptr->phases[i].isotopes[j].ratio_uncertainty; my_array[count_rows * max_column_count + (size_t)column] = -1.0; - sprintf(token, "%s %s", inv_ptr->phases[i].phase->name, + snprintf(token, sizeof(token), "%s %s", inv_ptr->phases[i].phase->name, "iso neg"); row_name[count_rows] = string_hsave(token); count_rows++; @@ -3784,7 +3784,7 @@ phase_isotope_inequalities(class inverse *inv_ptr) my_array[count_rows * max_column_count + (size_t)col_phases + (size_t)i] = -inv_ptr->phases[i].isotopes[j].ratio_uncertainty; my_array[count_rows * max_column_count + (size_t)column] = -1.0; - sprintf(token, "%s %s", inv_ptr->phases[i].phase->name, + snprintf(token, sizeof(token), "%s %s", inv_ptr->phases[i].phase->name, "iso pos"); row_name[count_rows] = string_hsave(token); count_rows++; @@ -3792,7 +3792,7 @@ phase_isotope_inequalities(class inverse *inv_ptr) my_array[count_rows * max_column_count + (size_t)col_phases + (size_t)i] = -inv_ptr->phases[i].isotopes[j].ratio_uncertainty; my_array[count_rows * max_column_count + (size_t)column] = 1.0; - sprintf(token, "%s %s", inv_ptr->phases[i].phase->name, + snprintf(token, sizeof(token), "%s %s", inv_ptr->phases[i].phase->name, "iso neg"); row_name[count_rows] = string_hsave(token); count_rows++; @@ -3829,7 +3829,7 @@ write_optimize_names(class inverse *inv_ptr) { for (i = 0; i < inv_ptr->count_solns; i++) { - sprintf(token, "%s %s %d", "optimize", + snprintf(token, sizeof(token), "%s %s %d", "optimize", inv_ptr->elts[j].master->elt->name, inv_ptr->solns[i]); row_name[row] = string_hsave(token); row++; @@ -3842,7 +3842,7 @@ write_optimize_names(class inverse *inv_ptr) { for (i = 0; i < inv_ptr->count_solns; i++) { - sprintf(token, "%s %s %d", "optimize", "pH", inv_ptr->solns[i]); + snprintf(token, sizeof(token), "%s %s %d", "optimize", "pH", inv_ptr->solns[i]); row_name[row] = string_hsave(token); row++; } @@ -3850,7 +3850,7 @@ write_optimize_names(class inverse *inv_ptr) /* * water */ - sprintf(token, "%s %s", "optimize", "water"); + snprintf(token, sizeof(token), "%s %s", "optimize", "water"); row_name[row] = string_hsave(token); row++; /* @@ -3860,7 +3860,7 @@ write_optimize_names(class inverse *inv_ptr) { for (j = 0; j < inv_ptr->isotope_unknowns.size(); j++) { - sprintf(token, "%s %d%s %d", "optimize", + snprintf(token, sizeof(token), "%s %d%s %d", "optimize", (int) inv_ptr->isotope_unknowns[j].isotope_number, inv_ptr->isotope_unknowns[j].elt_name, inv_ptr->solns[i]); row_name[row] = string_hsave(token); @@ -3875,7 +3875,7 @@ write_optimize_names(class inverse *inv_ptr) { for (j = 0; j < inv_ptr->isotopes.size(); j++) { - sprintf(token, "%s %s %d%s", "optimize", + snprintf(token, sizeof(token), "%s %s %d%s", "optimize", inv_ptr->phases[i].phase->name, (int) inv_ptr->isotopes[j].isotope_number, inv_ptr->isotopes[j].elt_name); diff --git a/phreeqcpp/kinetics.cpp b/phreeqcpp/kinetics.cpp index 27ce4bdc..f6e02338 100644 --- a/phreeqcpp/kinetics.cpp +++ b/phreeqcpp/kinetics.cpp @@ -1075,7 +1075,7 @@ rk_kinetics(int i, LDBLE kin_time, int use_mix, int nsaver, } { char str[MAX_LENGTH]; - sprintf(str, "RK-steps: Bad%4d. OK%5d. Time %3d%%", step_bad, + snprintf(str, sizeof(str), "RK-steps: Bad%4d. OK%5d. Time %3d%%", step_bad, step_ok, (int) (100 * h_sum / kin_time)); status(0, str, true); } diff --git a/phreeqcpp/mainsubs.cpp b/phreeqcpp/mainsubs.cpp index 0f1c714a..5cc76a3f 100644 --- a/phreeqcpp/mainsubs.cpp +++ b/phreeqcpp/mainsubs.cpp @@ -381,7 +381,7 @@ initial_solutions(int print) } if (print == TRUE) { - sprintf(token, "Initial solution %d.\t%.350s", + snprintf(token, sizeof(token), "Initial solution %d.\t%.350s", solution_ref.Get_n_user(), solution_ref.Get_description().c_str()); dup_print(token, FALSE); } @@ -518,7 +518,7 @@ initial_exchangers(int print) } if (print == TRUE) { - sprintf(token, "Exchange %d.\t%.350s", + snprintf(token, sizeof(token), "Exchange %d.\t%.350s", exchange_ptr->Get_n_user(), exchange_ptr->Get_description().c_str()); dup_print(token, FALSE); } @@ -609,7 +609,7 @@ initial_gas_phases(int print) } if (print == TRUE) { - sprintf(token, "Gas_Phase %d.\t%.350s", + snprintf(token, sizeof(token), "Gas_Phase %d.\t%.350s", gas_phase_ptr->Get_n_user(), gas_phase_ptr->Get_description().c_str()); dup_print(token, FALSE); } @@ -660,7 +660,7 @@ initial_gas_phases(int print) } if (fabs(gas_phase_ptr->Get_total_p() - use.Get_solution_ptr()->Get_patm()) > 5) { - sprintf(token, + snprintf(token, sizeof(token), "WARNING: While initializing gas phase composition by equilibrating:\n%s (%.2f atm) %s (%.2f atm).\n%s.", " Gas phase pressure", (double) gas_phase_ptr->Get_total_p(), @@ -828,7 +828,7 @@ reactions(void) for (reaction_step = 1; reaction_step <= count_steps; reaction_step++) { overall_iterations = 0; - sprintf(token, "Reaction step %d.", reaction_step); + snprintf(token, sizeof(token), "Reaction step %d.", reaction_step); if (reaction_step > 1 && incremental_reactions == FALSE) { copy_use(-2); @@ -934,7 +934,7 @@ saver(void) if (save.solution == TRUE) { - sprintf(token, "Solution after simulation %d.", simulation); + snprintf(token, sizeof(token), "Solution after simulation %d.", simulation); description_x = token; n = save.n_solution_user; xsolution_save(n); @@ -1025,7 +1025,7 @@ xexchange_save(int n_user) temp_exchange.Set_n_user(n_user); temp_exchange.Set_n_user_end(n_user); temp_exchange.Set_new_def(false); - sprintf(token, "Exchange assemblage after simulation %d.", simulation); + snprintf(token, sizeof(token), "Exchange assemblage after simulation %d.", simulation); temp_exchange.Set_description(token); temp_exchange.Set_solution_equilibria(false); temp_exchange.Set_n_solution(-999); @@ -1108,7 +1108,7 @@ xgas_save(int n_user) */ temp_gas_phase.Set_n_user(n_user); temp_gas_phase.Set_n_user_end(n_user); - sprintf(token, "Gas phase after simulation %d.", simulation); + snprintf(token, sizeof(token), "Gas phase after simulation %d.", simulation); temp_gas_phase.Set_description(token); temp_gas_phase.Set_new_def(false); temp_gas_phase.Set_solution_equilibria(false); @@ -2094,10 +2094,10 @@ run_simulations(void) #endif #if defined PHREEQCI_GUI - sprintf(token, "\nSimulation %d\n", simulation); + snprintf(token, sizeof(token), "\nSimulation %d\n", simulation); screen_msg(token); #endif - sprintf(token, "Reading input data for simulation %d.", simulation); + snprintf(token, sizeof(token), "Reading input data for simulation %d.", simulation); dup_print(token, TRUE); if (read_input() == EOF) @@ -2105,7 +2105,7 @@ run_simulations(void) if (title_x.size() > 0) { - sprintf(token, "TITLE"); + snprintf(token, sizeof(token), "TITLE"); dup_print(token, TRUE); if (pr.headings == TRUE) { diff --git a/phreeqcpp/parse.cpp b/phreeqcpp/parse.cpp index 82eb5400..6ea041d9 100644 --- a/phreeqcpp/parse.cpp +++ b/phreeqcpp/parse.cpp @@ -259,7 +259,7 @@ check_eqn(int association) /* ---------------------------------------------------------------------- */ int Phreeqc:: -get_charge(char *charge, LDBLE * l_z) +get_charge(char *charge, size_t charge_size, LDBLE * l_z) /* ---------------------------------------------------------------------- */ /* * Function takes character string and calculates the charge on @@ -367,7 +367,7 @@ get_charge(char *charge, LDBLE * l_z) */ if (abs(i) > 1) { - if (sprintf(charge, "%-+d", i) == EOF) + if (snprintf(charge, charge_size, "%-+d", i) == EOF) { error_string = sformatf( "Error converting charge to character string, %s.", diff --git a/phreeqcpp/pitzer.cpp b/phreeqcpp/pitzer.cpp index f2b6a92f..77de08e5 100644 --- a/phreeqcpp/pitzer.cpp +++ b/phreeqcpp/pitzer.cpp @@ -129,7 +129,7 @@ pitzer_tidy(void) { for (j = i + 1; j < count_cations; j++) { - sprintf(line, "%s %s 1", spec[i]->name, spec[j]->name); + snprintf(line, max_line, "%s %s 1", spec[i]->name, spec[j]->name); pzp_ptr = pitz_param_read(line, 2); pzp_ptr->type = TYPE_ETHETA; size_t count_pitz_param = pitz_params.size(); @@ -141,7 +141,7 @@ pitzer_tidy(void) { for (j = i + 1; j < 2 * (int)s.size() + count_anions; j++) { - sprintf(line, "%s %s 1", spec[i]->name, spec[j]->name); + snprintf(line, max_line, "%s %s 1", spec[i]->name, spec[j]->name); pzp_ptr = pitz_param_read(line, 2); pzp_ptr->type = TYPE_ETHETA; size_t count_pitz_param = pitz_params.size(); diff --git a/phreeqcpp/print.cpp b/phreeqcpp/print.cpp index 610b6c13..612460be 100644 --- a/phreeqcpp/print.cpp +++ b/phreeqcpp/print.cpp @@ -590,7 +590,7 @@ print_gas_phase(void) return (OK); if (gas_unknown->moles < 1e-12) { - sprintf(info, "Fixed-pressure gas phase %d dissolved completely", + snprintf(info, sizeof(info), "Fixed-pressure gas phase %d dissolved completely", use.Get_n_gas_phase_user()); print_centered(info); return (OK); @@ -1378,7 +1378,7 @@ print_pp_assemblage(void) x[j]->moles = 0.0; if (state != TRANSPORT && state != PHAST) { - sprintf(token, " %11.3e %11.3e %11.3e", + snprintf(token, sizeof(token), " %11.3e %11.3e %11.3e", (double) (comp_ptr->Get_moles() + comp_ptr->Get_delta()), (double) x[j]->moles, (double) (x[j]->moles - comp_ptr->Get_moles() - @@ -1386,7 +1386,7 @@ print_pp_assemblage(void) } else { - sprintf(token, " %11.3e %11.3e %11.3e", + snprintf(token, sizeof(token), " %11.3e %11.3e %11.3e", (double) comp_ptr->Get_initial_moles(), (double) x[j]->moles, (double) (x[j]->moles - comp_ptr->Get_initial_moles())); diff --git a/phreeqcpp/readtr.cpp b/phreeqcpp/readtr.cpp index 85d579ee..cf30b67d 100644 --- a/phreeqcpp/readtr.cpp +++ b/phreeqcpp/readtr.cpp @@ -749,21 +749,21 @@ read_transport(void) { if (max_cells == count_length) { - sprintf(token, + snprintf(token, sizeof(token), "Number of cells is increased to number of 'lengths' %d.", count_length); warning_msg(token); } else if (max_cells == count_disp) { - sprintf(token, + snprintf(token, sizeof(token), "Number of cells is increased to number of dispersivities %d.", count_disp); warning_msg(token); } else { - sprintf(token, + snprintf(token, sizeof(token), "Number of mobile cells is increased to (ceil)(number of porosities) / (1 + number of stagnant zones) = %d.", (int) ceil(((double)count_por / (1 + (double)stag_data.count_stag)))); warning_msg(token); @@ -1222,26 +1222,26 @@ dump_cpp(void) fs << "END" << "\n"; char token[MAX_LENGTH]; - sprintf(token, "KNOBS\n"); + snprintf(token, sizeof(token), "KNOBS\n"); fs << token; - sprintf(token, "\t-iter%15d\n", itmax); + snprintf(token, sizeof(token), "\t-iter%15d\n", itmax); fs << token; - sprintf(token, "\t-tol %15.3e\n", (double)ineq_tol); + snprintf(token, sizeof(token), "\t-tol %15.3e\n", (double)ineq_tol); fs << token; - sprintf(token, "\t-step%15.3e\n", (double)step_size); + snprintf(token, sizeof(token), "\t-step%15.3e\n", (double)step_size); fs << token; - sprintf(token, "\t-pe_s%15.3e\n", (double)pe_step_size); + snprintf(token, sizeof(token), "\t-pe_s%15.3e\n", (double)pe_step_size); fs << token; - sprintf(token, "\t-diag "); + snprintf(token, sizeof(token), "\t-diag "); fs << token; if (diagonal_scale == TRUE) { - sprintf(token, "true\n"); + snprintf(token, sizeof(token), "true\n"); fs << token; } else { - sprintf(token, "false\n"); + snprintf(token, sizeof(token), "false\n"); fs << token; } std::map < int, SelectedOutput >::iterator so_it = SelectedOutput_map.begin(); @@ -1249,223 +1249,223 @@ dump_cpp(void) { current_selected_output = &(so_it->second); - sprintf(token, "SELECTED_OUTPUT %d\n", current_selected_output->Get_n_user()); + snprintf(token, sizeof(token), "SELECTED_OUTPUT %d\n", current_selected_output->Get_n_user()); fs << token; - //sprintf(token, "\t-file %-15s\n", "sel_o$$$.prn"); + //snprintf(token, sizeof(token), "\t-file %-15s\n", "sel_o$$$.prn"); //fs << token; fs << "\t-file " << "sel_o$$$" << current_selected_output->Get_n_user() << ".prn\n"; //if (punch.count_totals != 0) if (current_selected_output->Get_totals().size() > 0) { - sprintf(token, "\t-tot "); + snprintf(token, sizeof(token), "\t-tot "); fs << token; for (size_t i = 0; i < current_selected_output->Get_totals().size(); i++) { - sprintf(token, " %s", current_selected_output->Get_totals()[i].first.c_str()); + snprintf(token, sizeof(token), " %s", current_selected_output->Get_totals()[i].first.c_str()); fs << token; } - sprintf(token, "\n"); + snprintf(token, sizeof(token), "\n"); fs << token; } if (current_selected_output->Get_molalities().size() > 0) { - sprintf(token, "\t-mol "); + snprintf(token, sizeof(token), "\t-mol "); fs << token; for (size_t i = 0; i < current_selected_output->Get_molalities().size(); i++) { - sprintf(token, " %s", current_selected_output->Get_molalities()[i].first.c_str()); + snprintf(token, sizeof(token), " %s", current_selected_output->Get_molalities()[i].first.c_str()); fs << token; } - sprintf(token, "\n"); + snprintf(token, sizeof(token), "\n"); fs << token; } if (current_selected_output->Get_activities().size() > 0) { - sprintf(token, "\t-act "); + snprintf(token, sizeof(token), "\t-act "); fs << token; for (size_t i = 0; i < current_selected_output->Get_activities().size(); i++) { - sprintf(token, " %s", current_selected_output->Get_activities()[i].first.c_str()); + snprintf(token, sizeof(token), " %s", current_selected_output->Get_activities()[i].first.c_str()); fs << token; } - sprintf(token, "\n"); + snprintf(token, sizeof(token), "\n"); fs << token; } if (current_selected_output->Get_pure_phases().size() > 0) { - sprintf(token, "\t-equ "); + snprintf(token, sizeof(token), "\t-equ "); fs << token; for (size_t i = 0; i < current_selected_output->Get_pure_phases().size(); i++) { - sprintf(token, " %s", current_selected_output->Get_pure_phases()[i].first.c_str()); + snprintf(token, sizeof(token), " %s", current_selected_output->Get_pure_phases()[i].first.c_str()); fs << token; } - sprintf(token, "\n"); + snprintf(token, sizeof(token), "\n"); fs << token; } if (current_selected_output->Get_si().size() > 0) { - sprintf(token, "\t-si "); + snprintf(token, sizeof(token), "\t-si "); fs << token; for (size_t i = 0; i < current_selected_output->Get_si().size(); i++) { - sprintf(token, " %s", current_selected_output->Get_si()[i].first.c_str()); + snprintf(token, sizeof(token), " %s", current_selected_output->Get_si()[i].first.c_str()); fs << token; } - sprintf(token, "\n"); + snprintf(token, sizeof(token), "\n"); fs << token; } if (current_selected_output->Get_gases().size() > 0) { - sprintf(token, "\t-gas "); + snprintf(token, sizeof(token), "\t-gas "); fs << token; for (size_t i = 0; i < current_selected_output->Get_gases().size(); i++) { - sprintf(token, " %s", current_selected_output->Get_gases()[i].first.c_str()); + snprintf(token, sizeof(token), " %s", current_selected_output->Get_gases()[i].first.c_str()); fs << token; } - sprintf(token, "\n"); + snprintf(token, sizeof(token), "\n"); fs << token; } if (current_selected_output->Get_s_s().size() > 0) { - sprintf(token, "\t-solid_solutions "); + snprintf(token, sizeof(token), "\t-solid_solutions "); fs << token; for (size_t i = 0; i < current_selected_output->Get_s_s().size(); i++) { - sprintf(token, " %s", current_selected_output->Get_s_s()[i].first.c_str()); + snprintf(token, sizeof(token), " %s", current_selected_output->Get_s_s()[i].first.c_str()); fs << token; } - sprintf(token, "\n"); + snprintf(token, sizeof(token), "\n"); fs << token; } if (current_selected_output->Get_kinetics().size() > 0) { - sprintf(token, "\t-kin "); + snprintf(token, sizeof(token), "\t-kin "); fs << token; for (size_t i = 0; i < current_selected_output->Get_kinetics().size(); i++) { - sprintf(token, " %s", current_selected_output->Get_kinetics()[i].first.c_str()); + snprintf(token, sizeof(token), " %s", current_selected_output->Get_kinetics()[i].first.c_str()); fs << token; } - sprintf(token, "\n"); + snprintf(token, sizeof(token), "\n"); fs << token; } } - sprintf(token, "TRANSPORT\n"); + snprintf(token, sizeof(token), "TRANSPORT\n"); fs << token; - sprintf(token, "\t-cells %6d\n", count_cells); + snprintf(token, sizeof(token), "\t-cells %6d\n", count_cells); fs << token; - sprintf(token, "\t-shifts%6d%6d\n", count_shifts, ishift); + snprintf(token, sizeof(token), "\t-shifts%6d%6d\n", count_shifts, ishift); fs << token; - sprintf(token, "\t-output_frequency %6d\n", print_modulus); + snprintf(token, sizeof(token), "\t-output_frequency %6d\n", print_modulus); fs << token; - sprintf(token, "\t-selected_output_frequency %6d\n", + snprintf(token, sizeof(token), "\t-selected_output_frequency %6d\n", punch_modulus); fs << token; - sprintf(token, "\t-bcon %6d%6d\n", bcon_first, bcon_last); + snprintf(token, sizeof(token), "\t-bcon %6d%6d\n", bcon_first, bcon_last); fs << token; - sprintf(token, "\t-timest %13.5e\n", (double)timest); + snprintf(token, sizeof(token), "\t-timest %13.5e\n", (double)timest); fs << token; if (!high_precision) { - sprintf(token, "\t-diffc %13.5e\n", (double)diffc); + snprintf(token, sizeof(token), "\t-diffc %13.5e\n", (double)diffc); fs << token; } else { - sprintf(token, "\t-diffc %20.12e\n", (double)diffc); + snprintf(token, sizeof(token), "\t-diffc %20.12e\n", (double)diffc); fs << token; } - sprintf(token, "\t-tempr %13.5e\n", (double)tempr); + snprintf(token, sizeof(token), "\t-tempr %13.5e\n", (double)tempr); fs << token; if (correct_disp == TRUE) { - sprintf(token, "\t-correct_disp %s\n", "True"); + snprintf(token, sizeof(token), "\t-correct_disp %s\n", "True"); fs << token; } else { - sprintf(token, "\t-correct_disp %s\n", "False"); + snprintf(token, sizeof(token), "\t-correct_disp %s\n", "False"); fs << token; } - sprintf(token, "\t-length\n"); + snprintf(token, sizeof(token), "\t-length\n"); fs << token; for (int i = 1; i <= count_cells; i++) { - sprintf(token, "%12.3e", (double)cell_data[i].length); + snprintf(token, sizeof(token), "%12.3e", (double)cell_data[i].length); fs << token; if (i > 0 && (i % 8) == 0) { - sprintf(token, "\n"); + snprintf(token, sizeof(token), "\n"); fs << token; } } - sprintf(token, "\n"); + snprintf(token, sizeof(token), "\n"); fs << token; - sprintf(token, "\t-disp\n"); + snprintf(token, sizeof(token), "\t-disp\n"); fs << token; for (int i = 1; i <= count_cells; i++) { if (!high_precision) { - sprintf(token, "%12.3e", (double)cell_data[i].disp); + snprintf(token, sizeof(token), "%12.3e", (double)cell_data[i].disp); fs << token; } else { - sprintf(token, "%20.12e", (double)cell_data[i].disp); + snprintf(token, sizeof(token), "%20.12e", (double)cell_data[i].disp); fs << token; } if (i > 0 && (i % 8) == 0) { - sprintf(token, "\n"); + snprintf(token, sizeof(token), "\n"); fs << token; } } - sprintf(token, "\n"); + snprintf(token, sizeof(token), "\n"); fs << token; - sprintf(token, "\t-punch_cells"); + snprintf(token, sizeof(token), "\t-punch_cells"); fs << token; l = 0; for (int i = 0; i < all_cells; i++) { if (cell_data[i].punch != TRUE) continue; - sprintf(token, " %d", i); + snprintf(token, sizeof(token), " %d", i); fs << token; l++; if ((l % 20) == 0) { - sprintf(token, "\n"); + snprintf(token, sizeof(token), "\n"); fs << token; } } - sprintf(token, "\n"); + snprintf(token, sizeof(token), "\n"); fs << token; - sprintf(token, "\t-print_cells"); + snprintf(token, sizeof(token), "\t-print_cells"); fs << token; l = 0; for (int i = 0; i < all_cells; i++) { if (cell_data[i].print != TRUE) continue; - sprintf(token, " %d", i); + snprintf(token, sizeof(token), " %d", i); fs << token; l++; if ((l % 20) == 0) { - sprintf(token, "\n"); + snprintf(token, sizeof(token), "\n"); fs << token; } } - sprintf(token, "\n"); + snprintf(token, sizeof(token), "\n"); fs << token; - sprintf(token, "\t-dump $$$.dmp\n"); + snprintf(token, sizeof(token), "\t-dump $$$.dmp\n"); fs << token; - sprintf(token, "\t-dump_frequency %d\n", dump_modulus); + snprintf(token, sizeof(token), "\t-dump_frequency %d\n", dump_modulus); fs << token; - sprintf(token, "\t-dump_restart %d\n", transport_step + 1); + snprintf(token, sizeof(token), "\t-dump_restart %d\n", transport_step + 1); fs << token; #if defined MULTICHART @@ -1473,7 +1473,7 @@ dump_cpp(void) chart_handler.dump(fs, 0); #endif - sprintf(token, "END\n"); + snprintf(token, sizeof(token), "END\n"); fs << token; return (OK); } diff --git a/phreeqcpp/transport.cpp b/phreeqcpp/transport.cpp index 32e0e0a8..36dda3cc 100644 --- a/phreeqcpp/transport.cpp +++ b/phreeqcpp/transport.cpp @@ -372,7 +372,7 @@ transport(void) /* multi_D calc's are OK if all cells have the same amount of water */ if (multi_Dflag == TRUE) { - sprintf(token, "multi_D calc's and stagnant: define MIXing factors explicitly, or \n\t give in -multi_D the Dw used for calculating the mobile-immobile exchange factor."); + snprintf(token, sizeof(token), "multi_D calc's and stagnant: define MIXing factors explicitly, or \n\t give in -multi_D the Dw used for calculating the mobile-immobile exchange factor."); warning_msg(token); } @@ -497,10 +497,10 @@ transport(void) * Now transport */ if (implicit) - sprintf(token, "\nCalculating implicit transport: %d (mobile) cells, %d shifts, %d mixruns, max. mixf = %g.\n\n", + snprintf(token, sizeof(token), "\nCalculating implicit transport: %d (mobile) cells, %d shifts, %d mixruns, max. mixf = %g.\n\n", count_cells, count_shifts - transport_start + 1, nmix, max_mixf); else - sprintf(token, "\nCalculating transport: %d (mobile) cells, %d shifts, %d mixruns...\n\n", + snprintf(token, sizeof(token), "\nCalculating transport: %d (mobile) cells, %d shifts, %d mixruns...\n\n", count_cells, count_shifts - transport_start + 1, nmix); warning_msg(token); max_iter = 0; @@ -544,14 +544,14 @@ transport(void) mixrun = j; if (multi_Dflag && j == floor((LDBLE)nmix / 2)) { - //sprintf(token, + //snprintf(token, sizeof(token), // "Transport step %3d. Multicomponent diffusion run %3d.", // transport_step, j); //dup_print(token, FALSE); } else if (!multi_Dflag) { - sprintf(token, "Transport step %3d. Mixrun %3d.", + snprintf(token, sizeof(token), "Transport step %3d. Mixrun %3d.", transport_step, j); dup_print(token, FALSE); } @@ -595,11 +595,11 @@ transport(void) max_iter = overall_iterations; cell_no = i; if (multi_Dflag) - sprintf(token, + snprintf(token, sizeof(token), "Transport step %3d. MCDrun %3d. Cell %3d. (Max. iter %3d)", transport_step, j, i, max_iter); else - sprintf(token, + snprintf(token, sizeof(token), "Transport step %3d. Mixrun %3d. Cell %3d. (Max. iter %3d)", transport_step, j, i, max_iter); status(0, token); @@ -665,7 +665,7 @@ transport(void) */ if (ishift != 0) { - sprintf(token, "Transport step %3d.", transport_step); + snprintf(token, sizeof(token), "Transport step %3d.", transport_step); dup_print(token, FALSE); if (b_c == 1) rate_sim_time_start = ((double)transport_step - 1) * @@ -758,11 +758,11 @@ transport(void) kin_time /= 2; cell_no = i; if (multi_Dflag) - sprintf(token, + snprintf(token, sizeof(token), "Transport step %3d. MCDrun %3d. Cell %3d. (Max. iter %3d)", transport_step, 0, i, max_iter); else - sprintf(token, + snprintf(token, sizeof(token), "Transport step %3d. Mixrun %3d. Cell %3d. (Max. iter %3d)", transport_step, 0, i, max_iter); status(0, token); @@ -807,14 +807,14 @@ transport(void) mixrun = j; if (multi_Dflag && j == nmix && (transport_step % print_modulus == 0)) { - sprintf(token, + snprintf(token, sizeof(token), "Transport step %3d. Multicomponent diffusion run %3d.", transport_step, j); dup_print(token, FALSE); } else if (!multi_Dflag) { - sprintf(token, "Transport step %3d. Mixrun %3d.", + snprintf(token, sizeof(token), "Transport step %3d. Mixrun %3d.", transport_step, j); dup_print(token, FALSE); } @@ -862,11 +862,11 @@ transport(void) max_iter = overall_iterations; cell_no = i; if (multi_Dflag) - sprintf(token, + snprintf(token, sizeof(token), "Transport step %3d. MCDrun %3d. Cell %3d. (Max. iter %3d)", transport_step, j, i, max_iter); else - sprintf(token, + snprintf(token, sizeof(token), "Transport step %3d. Mixrun %3d. Cell %3d. (Max. iter %3d)", transport_step, j, i, max_iter); status(0, token); @@ -959,14 +959,14 @@ transport(void) if (multi_Dflag && moles_added[0].moles > 0) { - sprintf(token, + snprintf(token, sizeof(token), "\nFor balancing negative concentrations in MCD, added in total to the system:"); warning_msg(token); for (i = 0; i < count_moles_added; i++) { if (!moles_added[i].moles) break; - sprintf(token, + snprintf(token, sizeof(token), "\t %.4e moles %s.", (double)moles_added[i].moles, moles_added[i].name); warning_msg(token); @@ -1246,7 +1246,7 @@ init_mix(void) m = (LDBLE *)free_check_null(m); m1 = (LDBLE *)free_check_null(m1); char token[MAX_LENGTH]; - sprintf(token, "Calculated number of mixes %g, is beyond program limit,\nERROR: please set implicit true, or decrease time_step, or increase cell-lengths.", 2.25 * maxmix); + snprintf(token, sizeof(token), "Calculated number of mixes %g, is beyond program limit,\nERROR: please set implicit true, or decrease time_step, or increase cell-lengths.", 2.25 * maxmix); error_msg(token, STOP); } if (bcon_first == 1 || bcon_last == 1) @@ -1367,7 +1367,7 @@ init_mix(void) m = (LDBLE *)free_check_null(m); m1 = (LDBLE *)free_check_null(m1); char token[MAX_LENGTH]; - sprintf(token, "Calculated number of mixes %g, is beyond program limit,\nERROR: please set implicit true, or decrease time_step, or increase cell-lengths.", 1.5 * maxmix); + snprintf(token, sizeof(token), "Calculated number of mixes %g, is beyond program limit,\nERROR: please set implicit true, or decrease time_step, or increase cell-lengths.", 1.5 * maxmix); error_msg(token, STOP); } l_nmix = 1 + (int) floor(1.5 * maxmix); @@ -1950,7 +1950,7 @@ fill_spec(int l_cell_no, int ref_cell) { if (!warn_MCD_X) { - sprintf(token, + snprintf(token, sizeof(token), "MCD found more than 1 exchanger, uses X for interlayer diffusion."); warning_msg(token); warn_MCD_X = 1; @@ -3593,7 +3593,7 @@ multi_D(LDBLE DDt, int mobile_cell, int stagnant) } if (temp < -1e-12) { - sprintf(token, + snprintf(token, sizeof(token), "Negative concentration in MCD: added %.4e moles %s in cell %d", (double)-temp, it->first.c_str(), i); warning_msg(token); diff --git a/phreeqcpp/utilities.cpp b/phreeqcpp/utilities.cpp index 67a030ee..d655f656 100644 --- a/phreeqcpp/utilities.cpp +++ b/phreeqcpp/utilities.cpp @@ -567,7 +567,7 @@ get_token(const char** eqnaddr, std::string& string, LDBLE* l_z, int* l) /* * Charge has been written, now need to check if charge has legal format */ - if (get_charge(charge, l_z) == OK) + if (get_charge(charge, MAX_LENGTH, l_z) == OK) { string.append(charge); } @@ -1193,37 +1193,37 @@ status(int count, const char *str, bool rk_string) { stdstr = str; } - sprintf(sim_str, "\rSimulation %d.", simulation); - sprintf(state_str, " "); - sprintf(spin_str, " "); + snprintf(sim_str, sizeof(sim_str), "\rSimulation %d.", simulation); + snprintf(state_str, sizeof(state_str), " "); + snprintf(spin_str, sizeof(spin_str), " "); switch (state) { default: break; case INITIAL_SOLUTION: - sprintf(state_str, "Initial solution %d.", use.Get_solution_ptr()->Get_n_user()); + snprintf(state_str, sizeof(state_str), "Initial solution %d.", use.Get_solution_ptr()->Get_n_user()); break; case INITIAL_EXCHANGE: - sprintf(state_str, "Initial exchange %d.", use.Get_exchange_ptr()->Get_n_user()); + snprintf(state_str, sizeof(state_str), "Initial exchange %d.", use.Get_exchange_ptr()->Get_n_user()); break; case INITIAL_SURFACE: - sprintf(state_str, "Initial surface %d.", use.Get_surface_ptr()->Get_n_user()); + snprintf(state_str, sizeof(state_str), "Initial surface %d.", use.Get_surface_ptr()->Get_n_user()); break; case INVERSE: - sprintf(state_str, "Inverse %d. Models = %d.", use.Get_inverse_ptr()->n_user, count); + snprintf(state_str, sizeof(state_str), "Inverse %d. Models = %d.", use.Get_inverse_ptr()->n_user, count); break; case REACTION: if (use.Get_kinetics_in() == TRUE) { - sprintf(state_str, "Kinetic step %d.", reaction_step); + snprintf(state_str, sizeof(state_str), "Kinetic step %d.", reaction_step); } else { - sprintf(state_str, "Reaction step %d.", reaction_step); + snprintf(state_str, sizeof(state_str), "Reaction step %d.", reaction_step); } break; case ADVECTION: - sprintf(state_str, "Advection, shift %d.", advection_step); + snprintf(state_str, sizeof(state_str), "Advection, shift %d.", advection_step); break; } spinner++; From a5c0125bc0670499ea06a874ed96f2e6fc5b3c45 Mon Sep 17 00:00:00 2001 From: Darth Vader Date: Sun, 15 Jan 2023 06:05:08 +0000 Subject: [PATCH 3/7] Squashed 'src/' changes from b32560ef..07fec0dd 07fec0dd Merge commit 'be9e230efb49f1bde06d9ca21a6a6042befc3c8e' be9e230e Squashed 'phreeqcpp/' changes from 8715a91..feaa432 git-subtree-dir: src git-subtree-split: 07fec0dda7375ac3e1c64cea372ebc7e7afe1013 From 0585087c9230a8e8e30c00f463c62dbf547717c5 Mon Sep 17 00:00:00 2001 From: Darth Vader Date: Tue, 17 Jan 2023 03:31:26 +0000 Subject: [PATCH 4/7] Squashed 'phreeqc3-examples/' changes from b674cfe5..4fbe69b0 4fbe69b0 [phreeqc3] Updated to split numdiff from memcheck added additional labels based on mytest/Makefile sorted lists in CMakeLists.txt files numdiff tests use Release builds git-subtree-dir: phreeqc3-examples git-subtree-split: 4fbe69b0b5e2b1d809367769096a79bffd027d48 --- CMakeLists.txt | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 46fcfe2e..ef1dc5bb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -275,12 +275,18 @@ add_test(NAME examples.ex22 COMMAND $ ${PROJECT_SOURCE_DIR}/examples/ex22 ex22.out ${PROJECT_SOURCE_DIR}/database/phreeqc.dat ex22.log ) -# these take more than 600 seconds -set(SKIP +# Note when setting labels we can't use the following: +# set_tests_properties(${test} PROPERTIES LABELS "pitzer") +# since it will overwrite any existing labels already set + +# long_debug +# > 600 seconds Debug +set(LONG_DEBUG examples.ex21 ) -# disable tests that take too long -foreach(test ${SKIP}) - set_tests_properties(${test} PROPERTIES DISABLED TRUE) +# label tests that take too long +foreach(test ${LONG_DEBUG}) + set_property(TEST ${test} APPEND PROPERTY LABELS "long_debug") + set_property(TEST ${test} APPEND PROPERTY LABELS "long_memcheck") endforeach() From 27d160eafc07e0c195861725fc0534960ae7e871 Mon Sep 17 00:00:00 2001 From: Darth Vader Date: Tue, 17 Jan 2023 03:32:45 +0000 Subject: [PATCH 5/7] Squashed 'src/' changes from 07fec0dd..c32e6641 c32e6641 Merge commit 'fb59ec8b19173e55422aef14fd2488cd1aca0680' fb59ec8b Squashed 'phreeqcpp/' changes from feaa432..bbd2543 git-subtree-dir: src git-subtree-split: c32e6641b2ffda0a8ea75f8b52327603923cf8b8 --- phreeqcpp/PBasic.cpp | 4 ++-- phreeqcpp/basicsubs.cpp | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/phreeqcpp/PBasic.cpp b/phreeqcpp/PBasic.cpp index 7233f91c..99b4661d 100644 --- a/phreeqcpp/PBasic.cpp +++ b/phreeqcpp/PBasic.cpp @@ -3610,7 +3610,7 @@ factor(struct LOC_exec * LINK) std::string std_num; { - snprintf(token, sizeof(token), "%*.*e", length, width, nmbr); + snprintf(token, max_length, "%*.*e", length, width, nmbr); std_num = token; } @@ -3653,7 +3653,7 @@ factor(struct LOC_exec * LINK) std::string std_num; { - snprintf(token, sizeof(token), "%*.*f", length, width, nmbr); + snprintf(token, max_length, "%*.*f", length, width, nmbr); std_num = token; } diff --git a/phreeqcpp/basicsubs.cpp b/phreeqcpp/basicsubs.cpp index 3e93470b..69eed0ed 100644 --- a/phreeqcpp/basicsubs.cpp +++ b/phreeqcpp/basicsubs.cpp @@ -2097,7 +2097,6 @@ match_elts_in_species(const char *name, const char *mytemplate) * write out string */ token[0] = '\0'; - assert(MAX_LENGTH == sizeof(token1)); for (i = 0; i < count_match_tokens; i++) { strcat(token, match_vector[i].first.c_str()); From 78e93f68cbab7b61521b50b47c82bfc9b3e77809 Mon Sep 17 00:00:00 2001 From: "Charlton, Scott R" Date: Mon, 16 Jan 2023 20:57:51 -0700 Subject: [PATCH 6/7] [iphreeqc] updated .gitignore --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 01e36937..c57ae923 100644 --- a/.gitignore +++ b/.gitignore @@ -211,4 +211,5 @@ doc/RELEASE /unit/x64 # builds (folders beginning with _ (underscore) -/_* \ No newline at end of file +**/_*/** +**/.vshistory/** From d942ba52110852e2c15cd178646c69cc5000bae5 Mon Sep 17 00:00:00 2001 From: "Charlton, Scott R" Date: Sun, 29 Jan 2023 00:53:31 -0700 Subject: [PATCH 7/7] Closes #14 and #15 (#16) * Added sessionInfo() to R-CMD-check * Set longer timeout for ctest * Added cron schedule in order to store htmlhelp cache --- .github/workflows/cmake.yml | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/.github/workflows/cmake.yml b/.github/workflows/cmake.yml index df191d65..58cb4eaa 100644 --- a/.github/workflows/cmake.yml +++ b/.github/workflows/cmake.yml @@ -5,6 +5,9 @@ on: pull_request: branches: [ master ] + schedule: + - cron: '15 14 4,11,18,25 * *' + env: # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.) BUILD_TYPE: Release @@ -56,7 +59,7 @@ jobs: arch: x64 - name: CTest - run: ctest -S ctest.cmake -V --output-on-failure + run: ctest -S ctest.cmake -V --output-on-failure --timeout 900 - name: Upload results uses: actions/upload-artifact@v3 @@ -75,9 +78,18 @@ jobs: repository: usgs-coupled-subtrees/phreeqc3-HTMLversion ref: master + - name: cache htmlhelp + id: cache-htmlhelp + uses: actions/cache@v3 + with: + path: "C:/Program Files (x86)/HTML Help Workshop" + key: ${{ runner.os }}-htmlhelp + - name: install htmlhelp + if: steps.cache-htmlhelp.outputs.cache-hit != 'true' + timeout-minutes: 5 run: | - curl -L -O http://web.archive.org/web/20160201063255/http://download.microsoft.com/download/0/A/9/0A939EF6-E31C-430F-A3DF-DFAE7960D564/htmlhelp.exe + curl -L -O --max-time 120 http://web.archive.org/web/20160201063255/http://download.microsoft.com/download/0/A/9/0A939EF6-E31C-430F-A3DF-DFAE7960D564/htmlhelp.exe cmd /c start /wait .\htmlhelp.exe /q /c /t:$(Get-Location)\tmp $file = "$(Get-Location)\tmp\htmlhelp.inf" (Get-Content $file) | Foreach-Object { $_ ` @@ -158,6 +170,11 @@ jobs: needs: roxygen2, pkgbuild, check working-directory: R/phreeqc + - name: Display sessionInfo() + run: sessionInfo() + working-directory: R/phreeqc + shell: Rscript {0} + - name: roxygen2::roxygenise() run: roxygen2::roxygenise() working-directory: R/phreeqc @@ -181,6 +198,7 @@ jobs: name: cran path: ${{ github.workspace }}/R/phreeqc/check/phreeqc_*.tar.gz + # r-build: # needs: [init] # runs-on: ubuntu-latest @@ -214,6 +232,7 @@ jobs: # name: cran # path: ${{github.workspace}}/R/phreeqc_*.tar.gz + # r-valgrind: # needs: [r-build] # runs-on: ubuntu-latest @@ -259,6 +278,7 @@ jobs: # name: valgrind # path: ${{github.workspace}}/R/valgrind.full.out + distcheck: needs: [init, chm] runs-on: ubuntu-latest