Repository: Unidata/netcdf-c (c)

Issue:
DAP Access of Zero-length Variable
As reported by Upanita Goswami at Mathworks, netCDF gives an error when accessing a dataset containing a zero-length variable.  The issue can be recreated with the current `master` development branch, and exists in netCDF-C `4.6.2`.  Recreating the issue with `ncdump` is easy, as shown below:

    $ ncdump http://test.opendap.org/opendap/data/nc/zero_length_array.nc

This command prints out the metadata, and then the following is observed:

````
data:

dim->dim.declsize0 > 0

Assertion failed: (dappanic("dim->dim.declsize0 > 0")), function dapvar2projection, file /Users/wfisher/Desktop/gitprojects/netcdf-c/libdap2/constraints.c, line 720.
 lat = 

Abort trap: 6 (core dumped)
````

@DennisHeimbigner is this expected behavior?  It looks like DAP is explicitly checking for zero-length variables, is this prohibited? If so, is there documentation to that effect somewhere? I will look for it myself shortly as well.

Pull request description:
When doing prefetch in DAP2, ignore invisible variables.
* Fixes #1300 

re: Issue https://github.com/Unidata/netcdf-c/issues/1300

Add a check for invisible variables.

Patch:
diff --git a/libdap2/cache.c b/libdap2/cache.c
index b05870058..88fc6221e 100644
--- a/libdap2/cache.c
+++ b/libdap2/cache.c
@@ -151,8 +151,10 @@ nclog(NCLOGDBG,"prefetch: %s",var->ncfullname);
     newconstraint->selections = dceclonelist(urlconstraint->selections);
 
     for(i=0;i<nclistlength(vars);i++) {
-        CDFnode* var = (CDFnode*)nclistget(vars,i);
 	DCEprojection* varprojection;
+        CDFnode* var = (CDFnode*)nclistget(vars,i);
+	/* Ignore invisible vars */
+	if(var->invisible) continue; 
 	/* convert var to a projection */
 	ncstat = dapvar2projection(var,&varprojection);
 	if(ncstat != NC_NOERR) {THROWCHK(ncstat); goto done;}


Test patch:
diff --git a/ncdap_test/CMakeLists.txt b/ncdap_test/CMakeLists.txt
index 3030f79f3..4f1093995 100644
--- a/ncdap_test/CMakeLists.txt
+++ b/ncdap_test/CMakeLists.txt
@@ -36,6 +36,7 @@ IF(ENABLE_TESTS)
     IF(BUILD_UTILITIES)
       add_sh_test(ncdap tst_ber)
       add_sh_test(ncdap tst_remote3)
+      add_sh_test(ncdap tst_zero_len_var)
 # not yet      add_sh_test(ncdap tst_hyrax)
       add_sh_test(ncdap tst_fillmismatch)
       IF(ENABLE_DAP_LONG_TESTS)
diff --git a/ncdap_test/Makefile.am b/ncdap_test/Makefile.am
index 1e38959b1..1fa5b5023 100644
--- a/ncdap_test/Makefile.am
+++ b/ncdap_test/Makefile.am
@@ -42,7 +42,7 @@ check_PROGRAMS += findtestserver
 findtestserver_SOURCES = findtestserver.c
 
 if BUILD_UTILITIES
-TESTS += tst_ber.sh tst_remote3.sh tst_formatx.sh testurl.sh tst_fillmismatch.sh
+TESTS += tst_ber.sh tst_remote3.sh tst_formatx.sh testurl.sh tst_fillmismatch.sh tst_zero_len_var.sh
 endif
 
 TESTS += test_partvar
@@ -83,6 +83,7 @@ SUBDIRS = testdata3 expected3 expectremote3
 EXTRA_DIST = tst_ncdap3.sh  \
              tst_remote3.sh \
              tst_longremote3.sh \
+			 tst_zero_len_var.sh \
              tst_filelists.sh tst_urls.sh tst_utils.sh \
 	     t_dap.c CMakeLists.txt tst_formatx.sh testauth.sh testurl.sh \
 	     t_ncf330.c tst_ber.sh tst_fillmismatch.sh \
diff --git a/ncdap_test/tst_zero_len_var.sh b/ncdap_test/tst_zero_len_var.sh
new file mode 100755
index 000000000..22ef9b7df
--- /dev/null
+++ b/ncdap_test/tst_zero_len_var.sh
@@ -0,0 +1,24 @@
+#!/bin/sh
+
+if test "x$SETX" != x ; then set -x; fi
+set -e
+
+if test "x$srcdir" = x ; then srcdir=`pwd`; fi
+. ../test_common.sh
+
+##
+# If the bug referenced in https://github.com/Unidata/netcdf-c/issues/1300
+# reoccurs, then the following command would fail.
+
+${NCDUMP} http://test.opendap.org/opendap/data/nc/zero_length_array.nc > tst_zero_len_var.cdl
+
+RES=$?
+
+if [ $RES -ne 0 ]; then
+    echo "Error $RES"
+    exit $RES
+fi
+
+rm -f tst_zero_len_var.cdl
+
+exit 0