Language: C
Label: safe
CWE: CWE-190 \u2014 Integer Overflow or Wraparound
The product performs a calculation that can produce an integer overflow or wraparound, when the logic assumes that the resulting value will always be larger than the original value. This can introduce other weaknesses when the calculation is used for resource management or execution control.

Code:
int cp_pbpsi_ask(g2_t d[], bn_t r, const bn_t x[], const g2_t s[], size_t m) {
	int i, result = RLC_OK;
	bn_t t, q, *p = RLC_ALLOCA(bn_t, m + 1), *_x = RLC_ALLOCA(bn_t, m + 1);

	bn_null(q);
	bn_null(t);

	RLC_TRY {
		bn_new(q);
		bn_new(t);
		if (p == NULL) {
			RLC_THROW(ERR_NO_MEMORY);
		}
		for (i = 0; i <= m; i++) {
			bn_null(p[i]);
			bn_new(p[i]);
			bn_null(_x[i]);
			bn_new(_x[i]);
		}

		pc_get_ord(q);
		bn_rand_mod(r, q);
		if (m == 0) {
			g2_mul_gen(d[0], r);
		} else {
			bn_lag(p, x, q, m);
			g2_mul_sim_lot(d[0], s, p, m + 1);
			g2_mul(d[0], d[0], r);
			for (i = 0; i < m; i++) {
				bn_copy(_x[i], x[i]);
			}
			for (i = 0; i < m; i++) {
				bn_copy(t, _x[i]);
				bn_copy(_x[i], _x[m - 1]);
				bn_lag(p, _x, q, m - 1);
				g2_mul_sim_lot(d[i + 1], s, p, m);
				g2_mul(d[i + 1], d[i + 1], r);
				bn_copy(_x[i], t);
			}
		}
	}
	RLC_CATCH_ANY {
		result = RLC_ERR;
	}
	RLC_FINALLY {
		bn_free(q);
		bn_free(t);
		for (i = 0; i <= m; i++) {
			bn_free(p[i]);
			bn_free(_x[i]);
		}
		RLC_FREE(p);
		RLC_FREE(_x);
	}
	return result;
}