astropy: WCS: missing error check for zero coordinates (ncoord)

src

int
pipeline_all_pixel2world(
    pipeline_t* pipeline,
    const unsigned int ncoord,
    const unsigned int nelem,
    const double* const pixcrd /* [ncoord][nelem] */,
    double* world /* [ncoord][nelem] */) {

...

if (has_wcs) {
    buffer = mem = malloc(
        ncoord * nelem * sizeof(double) + /* imgcrd */
        ncoord * sizeof(double) +         /* phi */
        ncoord * sizeof(double) +         /* theta */
        ncoord * nelem * sizeof(double) + /* tmp */
        ncoord * nelem * sizeof(int)      /* stat */
        );

    if (buffer == NULL) {
      status = wcserr_set(
        PIP_ERRMSG(WCSERR_MEMORY), "Memory allocation failed");
      goto exit;
    }
...

Shouldn’t there be a check for ncoord > 0 before being used for allocation size? malloc can still accept a zero size so I’m not sure if this intentional or a bug? c.f. https://en.cppreference.com/w/c/memory/malloc

“If size is zero, the behavior is implementation defined (null pointer may be returned, or some non-null pointer may be returned that may not be used to access storage, but has to be passed to free).”

This trips up in the subsequent call to wcsp2s here with the following error (src):

...
/* Sanity check. */
  if (ncoord < 1 || (ncoord > 1 && nelem < wcs->naxis)) {
    return wcserr_set(WCSERR_SET(WCSERR_BAD_CTYPE),
      "ncoord and/or nelem inconsistent with the wcsprm");
  }
...

Inspecting the values a this point for my example ncoord == 0. (BTW: it’s a good idea for errors to print values and boundaries.)

This is a similar error as reported in https://github.com/astropy/astropy/issues/4973 “Don’t raise an error if passing zero coordinates to WCS”, excpet that it is in wcsp2s and not wcss2p.

InconsistentAxisTypesError: ERROR 4 in wcsp2s() at line 2647 of file cextern/wcslib/C/wcs.c:
ncoord and/or nelem inconsistent with the wcsprm.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 19 (19 by maintainers)

Commits related to this issue

Most upvoted comments

Agreed this should be fixed. If your use case is different from calling all_pix2world or all_world2pix, yes, please post it here. I don’t worry too much about how spread this could have been because it’s a bit of a superficial use case which came up (I think) recently when we started writing tools that use these functions. It’s unusual to call those methods with empty arrays.

Before this is merged, could you change the error message to WCSERR_BAD_PIX?