aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--miscutils/bc.c43
1 files changed, 19 insertions, 24 deletions
diff --git a/miscutils/bc.c b/miscutils/bc.c
index cc15a8dd2..500d97123 100644
--- a/miscutils/bc.c
+++ b/miscutils/bc.c
@@ -5092,7 +5092,7 @@ static BcVec* xc_program_search(char *id, bool var)
5092} 5092}
5093 5093
5094// 'num' need not be initialized on entry 5094// 'num' need not be initialized on entry
5095static BC_STATUS zxc_program_num(BcResult *r, BcNum **num, bool hex) 5095static BC_STATUS zxc_program_num(BcResult *r, BcNum **num)
5096{ 5096{
5097 switch (r->t) { 5097 switch (r->t) {
5098 case XC_RESULT_STR: 5098 case XC_RESULT_STR:
@@ -5105,7 +5105,6 @@ static BC_STATUS zxc_program_num(BcResult *r, BcNum **num, bool hex)
5105 case XC_RESULT_CONSTANT: { 5105 case XC_RESULT_CONSTANT: {
5106 BcStatus s; 5106 BcStatus s;
5107 char *str; 5107 char *str;
5108 unsigned base_t;
5109 size_t len; 5108 size_t len;
5110 5109
5111 str = *xc_program_const(r->d.id.idx); 5110 str = *xc_program_const(r->d.id.idx);
@@ -5113,9 +5112,7 @@ static BC_STATUS zxc_program_num(BcResult *r, BcNum **num, bool hex)
5113 5112
5114 bc_num_init(&r->d.n, len); 5113 bc_num_init(&r->d.n, len);
5115 5114
5116 hex = hex && len == 1; 5115 s = zxc_num_parse(&r->d.n, str, G.prog.ib_t);
5117 base_t = hex ? 16 : G.prog.ib_t;
5118 s = zxc_num_parse(&r->d.n, str, base_t);
5119 if (s) { 5116 if (s) {
5120 bc_num_free(&r->d.n); 5117 bc_num_free(&r->d.n);
5121 RETURN_STATUS(s); 5118 RETURN_STATUS(s);
@@ -5162,7 +5159,6 @@ static BC_STATUS zxc_program_binOpPrep(BcResult **l, BcNum **ln,
5162 BcResult **r, BcNum **rn, bool assign) 5159 BcResult **r, BcNum **rn, bool assign)
5163{ 5160{
5164 BcStatus s; 5161 BcStatus s;
5165 bool hex;
5166 BcResultType lt, rt; 5162 BcResultType lt, rt;
5167 5163
5168 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1)) 5164 if (!STACK_HAS_MORE_THAN(&G.prog.results, 1))
@@ -5171,19 +5167,18 @@ static BC_STATUS zxc_program_binOpPrep(BcResult **l, BcNum **ln,
5171 *r = bc_vec_item_rev(&G.prog.results, 0); 5167 *r = bc_vec_item_rev(&G.prog.results, 0);
5172 *l = bc_vec_item_rev(&G.prog.results, 1); 5168 *l = bc_vec_item_rev(&G.prog.results, 1);
5173 5169
5174 lt = (*l)->t; 5170 s = zxc_program_num(*l, ln);
5175 rt = (*r)->t;
5176 hex = assign && (lt == XC_RESULT_IBASE || lt == XC_RESULT_OBASE);
5177
5178 s = zxc_program_num(*l, ln, false);
5179 if (s) RETURN_STATUS(s); 5171 if (s) RETURN_STATUS(s);
5180 s = zxc_program_num(*r, rn, hex); 5172 s = zxc_program_num(*r, rn);
5181 if (s) RETURN_STATUS(s); 5173 if (s) RETURN_STATUS(s);
5182 5174
5175 lt = (*l)->t;
5176 rt = (*r)->t;
5177
5183 // We run this again under these conditions in case any vector has been 5178 // We run this again under these conditions in case any vector has been
5184 // reallocated out from under the BcNums or arrays we had. 5179 // reallocated out from under the BcNums or arrays we had.
5185 if (lt == rt && (lt == XC_RESULT_VAR || lt == XC_RESULT_ARRAY_ELEM)) { 5180 if (lt == rt && (lt == XC_RESULT_VAR || lt == XC_RESULT_ARRAY_ELEM)) {
5186 s = zxc_program_num(*l, ln, false); 5181 s = zxc_program_num(*l, ln);
5187 if (s) RETURN_STATUS(s); 5182 if (s) RETURN_STATUS(s);
5188 } 5183 }
5189 5184
@@ -5212,7 +5207,7 @@ static BC_STATUS zxc_program_prep(BcResult **r, BcNum **n)
5212 RETURN_STATUS(bc_error_stack_has_too_few_elements()); 5207 RETURN_STATUS(bc_error_stack_has_too_few_elements());
5213 *r = bc_vec_top(&G.prog.results); 5208 *r = bc_vec_top(&G.prog.results);
5214 5209
5215 s = zxc_program_num(*r, n, false); 5210 s = zxc_program_num(*r, n);
5216 if (s) RETURN_STATUS(s); 5211 if (s) RETURN_STATUS(s);
5217 5212
5218 if (!BC_PROG_NUM((*r), (*n))) 5213 if (!BC_PROG_NUM((*r), (*n)))
@@ -5590,7 +5585,7 @@ static BC_STATUS zxc_program_print(char inst, size_t idx)
5590 RETURN_STATUS(bc_error_stack_has_too_few_elements()); 5585 RETURN_STATUS(bc_error_stack_has_too_few_elements());
5591 5586
5592 r = bc_vec_item_rev(&G.prog.results, idx); 5587 r = bc_vec_item_rev(&G.prog.results, idx);
5593 s = zxc_program_num(r, &num, false); 5588 s = zxc_program_num(r, &num);
5594 if (s) RETURN_STATUS(s); 5589 if (s) RETURN_STATUS(s);
5595 5590
5596 if (BC_PROG_NUM(r, num)) { 5591 if (BC_PROG_NUM(r, num)) {
@@ -5739,7 +5734,7 @@ static BC_STATUS zxc_program_copyToVar(char *name, bool var)
5739 RETURN_STATUS(zdc_program_assignStr(ptr, v, true)); 5734 RETURN_STATUS(zdc_program_assignStr(ptr, v, true));
5740#endif 5735#endif
5741 5736
5742 s = zxc_program_num(ptr, &n, false); 5737 s = zxc_program_num(ptr, &n);
5743 if (s) RETURN_STATUS(s); 5738 if (s) RETURN_STATUS(s);
5744 5739
5745 // Do this once more to make sure that pointers were not invalidated. 5740 // Do this once more to make sure that pointers were not invalidated.
@@ -6031,7 +6026,7 @@ static BC_STATUS zbc_program_return(char inst)
6031 BcNum *num; 6026 BcNum *num;
6032 BcResult *operand = bc_vec_top(&G.prog.results); 6027 BcResult *operand = bc_vec_top(&G.prog.results);
6033 6028
6034 s = zxc_program_num(operand, &num, false); 6029 s = zxc_program_num(operand, &num);
6035 if (s) RETURN_STATUS(s); 6030 if (s) RETURN_STATUS(s);
6036 bc_num_init(&res.d.n, num->len); 6031 bc_num_init(&res.d.n, num->len);
6037 bc_num_copy(&res.d.n, num); 6032 bc_num_copy(&res.d.n, num);
@@ -6087,7 +6082,7 @@ static BC_STATUS zxc_program_builtin(char inst)
6087 RETURN_STATUS(bc_error_stack_has_too_few_elements()); 6082 RETURN_STATUS(bc_error_stack_has_too_few_elements());
6088 opnd = bc_vec_top(&G.prog.results); 6083 opnd = bc_vec_top(&G.prog.results);
6089 6084
6090 s = zxc_program_num(opnd, &num, false); 6085 s = zxc_program_num(opnd, &num);
6091 if (s) RETURN_STATUS(s); 6086 if (s) RETURN_STATUS(s);
6092 6087
6093#if ENABLE_DC 6088#if ENABLE_DC
@@ -6163,7 +6158,7 @@ static BC_STATUS zdc_program_modexp(void)
6163 if (s) RETURN_STATUS(s); 6158 if (s) RETURN_STATUS(s);
6164 6159
6165 r1 = bc_vec_item_rev(&G.prog.results, 2); 6160 r1 = bc_vec_item_rev(&G.prog.results, 2);
6166 s = zxc_program_num(r1, &n1, false); 6161 s = zxc_program_num(r1, &n1);
6167 if (s) RETURN_STATUS(s); 6162 if (s) RETURN_STATUS(s);
6168 if (!BC_PROG_NUM(r1, n1)) 6163 if (!BC_PROG_NUM(r1, n1))
6169 RETURN_STATUS(bc_error_variable_is_wrong_type()); 6164 RETURN_STATUS(bc_error_variable_is_wrong_type());
@@ -6171,11 +6166,11 @@ static BC_STATUS zdc_program_modexp(void)
6171 // Make sure that the values have their pointers updated, if necessary. 6166 // Make sure that the values have their pointers updated, if necessary.
6172 if (r1->t == XC_RESULT_VAR || r1->t == XC_RESULT_ARRAY_ELEM) { 6167 if (r1->t == XC_RESULT_VAR || r1->t == XC_RESULT_ARRAY_ELEM) {
6173 if (r1->t == r2->t) { 6168 if (r1->t == r2->t) {
6174 s = zxc_program_num(r2, &n2, false); 6169 s = zxc_program_num(r2, &n2);
6175 if (s) RETURN_STATUS(s); 6170 if (s) RETURN_STATUS(s);
6176 } 6171 }
6177 if (r1->t == r3->t) { 6172 if (r1->t == r3->t) {
6178 s = zxc_program_num(r3, &n3, false); 6173 s = zxc_program_num(r3, &n3);
6179 if (s) RETURN_STATUS(s); 6174 if (s) RETURN_STATUS(s);
6180 } 6175 }
6181 } 6176 }
@@ -6220,7 +6215,7 @@ static BC_STATUS zdc_program_asciify(void)
6220 RETURN_STATUS(bc_error_stack_has_too_few_elements()); 6215 RETURN_STATUS(bc_error_stack_has_too_few_elements());
6221 r = bc_vec_top(&G.prog.results); 6216 r = bc_vec_top(&G.prog.results);
6222 6217
6223 s = zxc_program_num(r, &num, false); 6218 s = zxc_program_num(r, &num);
6224 if (s) RETURN_STATUS(s); 6219 if (s) RETURN_STATUS(s);
6225 6220
6226 if (BC_PROG_NUM(r, num)) { 6221 if (BC_PROG_NUM(r, num)) {
@@ -6284,7 +6279,7 @@ static BC_STATUS zdc_program_printStream(void)
6284 RETURN_STATUS(bc_error_stack_has_too_few_elements()); 6279 RETURN_STATUS(bc_error_stack_has_too_few_elements());
6285 r = bc_vec_top(&G.prog.results); 6280 r = bc_vec_top(&G.prog.results);
6286 6281
6287 s = zxc_program_num(r, &n, false); 6282 s = zxc_program_num(r, &n);
6288 if (s) RETURN_STATUS(s); 6283 if (s) RETURN_STATUS(s);
6289 6284
6290 if (BC_PROG_NUM(r, n)) { 6285 if (BC_PROG_NUM(r, n)) {
@@ -6379,7 +6374,7 @@ static BC_STATUS zdc_program_execStr(char *code, size_t *bgn, bool cond)
6379 sidx = r->d.id.idx; 6374 sidx = r->d.id.idx;
6380 } else if (r->t == XC_RESULT_VAR) { 6375 } else if (r->t == XC_RESULT_VAR) {
6381 BcNum *n; 6376 BcNum *n;
6382 s = zxc_program_num(r, &n, false); 6377 s = zxc_program_num(r, &n);
6383 if (s || !BC_PROG_STR(n)) goto exit; 6378 if (s || !BC_PROG_STR(n)) goto exit;
6384 sidx = n->rdx; 6379 sidx = n->rdx;
6385 } else 6380 } else