D4108: index: extract a type for the nodetree

Yuya Nishihara yuya at tcha.org
Mon Aug 6 08:52:07 EDT 2018


Queued, but there seem to be a couple of minor errors. Can you fix them
if they remain in the code after the refactor?

>  static int nt_new(indexObject *self)
>  {
>  	if (self->ntlength == self->ntcapacity) {
> -		if (self->ntcapacity >= INT_MAX / (sizeof(nodetree) * 2)) {
> +		if (self->ntcapacity >= INT_MAX / (sizeof(nodetreenode) * 2)) {
>  			PyErr_SetString(PyExc_MemoryError,
>  					"overflow in nt_new");
>  			return -1;
>  		}
>  		self->ntcapacity *= 2;
> -		self->nt = realloc(self->nt,
> -				   self->ntcapacity * sizeof(nodetree));
> -		if (self->nt == NULL) {
> +		self->nt->nodes = realloc(self->nt->nodes,
> +					  self->ntcapacity * sizeof(nodetreenode));
> +		if (self->nt->nodes == NULL) {
>  			PyErr_SetString(PyExc_MemoryError, "out of memory");
>  			return -1;

Need to free and nullify `self->nt` here?
Leaving `self->nt && !self->nt->nodes` state will probably lead to SEVG.

Another option is to leave `ntcapacity` and `nodes` unmodified on realloc
failure.

>  static int nt_init(indexObject *self)
>  {
>  	if (self->nt == NULL) {
> -		if ((size_t)self->raw_length > INT_MAX / sizeof(nodetree)) {
> +		self->nt = PyMem_Malloc(sizeof(nodetree));

`PyMem_Malloc()` and `PyMem_Free()` should be paired.

> +		if ((size_t)self->raw_length > INT_MAX / sizeof(nodetreenode)) {
>  			PyErr_SetString(PyExc_ValueError, "overflow in nt_init");

Need to free and nullify `self->nt` here?


More information about the Mercurial-devel mailing list