question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

WCS: missing error check for zero coordinates (ncoord)

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:19 (19 by maintainers)

github_iconTop GitHub Comments

1reaction
ndencommented, Sep 8, 2018

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?

0reactions
jamienosscommented, Sep 8, 2018

If your use case is different from calling all_pix2world or all_world2pix, yes, please post it here.

It isn’t.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Wcsprm — Astropy v5.2
Transforms world coordinates to pixel coordinates. set (). Sets up a WCS object for use according to information supplied within it.
Read more >
WCSLIB: wcs.h File Reference
Routines in this suite implement the FITS World Coordinate System (WCS) standard which defines methods to be used for computing world coordinates from...
Read more >
Wcsprm — Astropy v0.4.2
Wcsprm is a direct wrapper around wcslib. It provides access to the core WCS transformations that it supports. Note. The members of this...
Read more >
AST A Library for Handling World Coordinate ... - Starlink
Test if a WCS is a Celestial Coordinate System . ... Error Handling. Error messages are written to standard error by default, but...
Read more >
WCSLIB 4.4 Reference Manual
wcsprm (Coordinate transformation parameters ) ... the projection is at native latitude zero, its celestial latitude is zero, and LONPOLEa ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found