Michael W. Bombardieri wrote:
> Hi tech@,
>
> In dc(1) the function array_free() knows how to skip a null pointer so
> calling code doesn't need to avoid passing null.
> Also, minor style (braces) clean-up.
ok mmcc@
It may also be a good idea to change the allocation names in the form
bmalloc -> xmalloc. It'd make it more obvious what they do. Just
nitpicking, though.
> Index: bcode.c
> ===================================================================
> RCS file: /cvs/src/usr.bin/dc/bcode.c,v
> retrieving revision 1.48
> diff -u -p -u -r1.48 bcode.c
> --- bcode.c 3 Oct 2015 16:24:53 -0000 1.48
> +++ bcode.c 10 Nov 2015 03:50:43 -0000
> @@ -937,9 +937,8 @@ badd(void)
> struct number *r;
>
> a = pop_number();
> - if (a == NULL) {
> + if (a == NULL)
> return;
> - }
> b = pop_number();
> if (b == NULL) {
> push_number(a);
> @@ -965,9 +964,8 @@ bsub(void)
> struct number *r;
>
> a = pop_number();
> - if (a == NULL) {
> + if (a == NULL)
> return;
> - }
> b = pop_number();
> if (b == NULL) {
> push_number(a);
> @@ -1014,9 +1012,8 @@ bmul(void)
> struct number *r;
>
> a = pop_number();
> - if (a == NULL) {
> + if (a == NULL)
> return;
> - }
> b = pop_number();
> if (b == NULL) {
> push_number(a);
> @@ -1040,9 +1037,8 @@ bdiv(void)
> BN_CTX *ctx;
>
> a = pop_number();
> - if (a == NULL) {
> + if (a == NULL)
> return;
> - }
> b = pop_number();
> if (b == NULL) {
> push_number(a);
> @@ -1078,9 +1074,8 @@ bmod(void)
> BN_CTX *ctx;
>
> a = pop_number();
> - if (a == NULL) {
> + if (a == NULL)
> return;
> - }
> b = pop_number();
> if (b == NULL) {
> push_number(a);
> @@ -1116,9 +1111,8 @@ bdivmod(void)
> BN_CTX *ctx;
>
> a = pop_number();
> - if (a == NULL) {
> + if (a == NULL)
> return;
> - }
> b = pop_number();
> if (b == NULL) {
> push_number(a);
> @@ -1158,9 +1152,8 @@ bexp(void)
> u_int rscale;
>
> p = pop_number();
> - if (p == NULL) {
> + if (p == NULL)
> return;
> - }
> a = pop_number();
> if (a == NULL) {
> push_number(p);
> @@ -1282,9 +1275,8 @@ bsqrt(void)
>
> onecount = 0;
> n = pop_number();
> - if (n == NULL) {
> + if (n == NULL)
> return;
> - }
> if (BN_is_zero(n->number)) {
> r = new_number();
> push_number(r);
> @@ -1325,9 +1317,8 @@ not(void)
> struct number *a;
>
> a = pop_number();
> - if (a == NULL) {
> + if (a == NULL)
> return;
> - }
> a->scale = 0;
> bn_check(BN_set_word(a->number, BN_get_word(a->number) ? 0 : 1));
> push_number(a);
> @@ -1345,9 +1336,8 @@ equal_numbers(void)
> struct number *a, *b, *r;
>
> a = pop_number();
> - if (a == NULL) {
> + if (a == NULL)
> return;
> - }
> b = pop_number();
> if (b == NULL) {
> push_number(a);
> @@ -1365,9 +1355,8 @@ less_numbers(void)
> struct number *a, *b, *r;
>
> a = pop_number();
> - if (a == NULL) {
> + if (a == NULL)
> return;
> - }
> b = pop_number();
> if (b == NULL) {
> push_number(a);
> @@ -1385,9 +1374,8 @@ lesseq_numbers(void)
> struct number *a, *b, *r;
>
> a = pop_number();
> - if (a == NULL) {
> + if (a == NULL)
> return;
> - }
> b = pop_number();
> if (b == NULL) {
> push_number(a);
> @@ -1711,9 +1699,8 @@ eval_tos(void)
> char *p;
>
> p = pop_string();
> - if (p == NULL)
> - return;
> - eval_string(p);
> + if (p != NULL)
> + eval_string(p);
> }
>
> void
> Index: stack.c
> ===================================================================
> RCS file: /cvs/src/usr.bin/dc/stack.c,v
> retrieving revision 1.13
> diff -u -p -u -r1.13 stack.c
> --- stack.c 1 Dec 2014 13:13:00 -0000 1.13
> +++ stack.c 10 Nov 2015 03:50:43 -0000
> @@ -62,10 +62,8 @@ stack_free_value(struct value *v)
> free(v->u.string);
> break;
> }
> - if (v->array != NULL) {
> - array_free(v->array);
> - v->array = NULL;
> - }
> + array_free(v->array);
> + v->array = NULL;
> }
>
> /* Copy number or string content into already allocated target */
> @@ -210,10 +208,8 @@ stack_popnumber(struct stack *stack)
> {
> if (stack_empty(stack))
> return NULL;
> - if (stack->stack[stack->sp].array != NULL) {
> - array_free(stack->stack[stack->sp].array);
> - stack->stack[stack->sp].array = NULL;
> - }
> + array_free(stack->stack[stack->sp].array);
> + stack->stack[stack->sp].array = NULL;
> if (stack->stack[stack->sp].type != BCODE_NUMBER) {
> warnx("not a number"); /* XXX remove */
> return NULL;
> @@ -226,10 +222,8 @@ stack_popstring(struct stack *stack)
> {
> if (stack_empty(stack))
> return NULL;
> - if (stack->stack[stack->sp].array != NULL) {
> - array_free(stack->stack[stack->sp].array);
> - stack->stack[stack->sp].array = NULL;
> - }
> + array_free(stack->stack[stack->sp].array);
> + stack->stack[stack->sp].array = NULL;
> if (stack->stack[stack->sp].type != BCODE_STRING) {
> warnx("not a string"); /* XXX remove */
> return NULL;
> @@ -240,9 +234,8 @@ stack_popstring(struct stack *stack)
> void
> stack_clear(struct stack *stack)
> {
> - while (stack->sp >= 0) {
> + while (stack->sp >= 0)
> stack_free_value(&stack->stack[stack->sp--]);
> - }
> free(stack->stack);
> stack_init(stack);
> }
>