Qstat -x -f -F json (Job_Name = NaN bug)

In a pbs job script, when I set Job_Name to NaN(#PBS -N NaN),
the value of Job_Name will be nan without double quote in JSON format
of qstat output(qstat -x -f -F JSON), as follows.

Usually, it seems to be enclosed in double quote. (“Job_Name”:“test”,)

/—
$ qstat -x -f -F JSON
{
“timestamp”:---------,
“pbs_version”:“19.1.3”,
“pbs_server”:pbs-srv",
“Jobs”:{
“XXXXX.pbs”:{
“Job_Name”:“test”,
“Job_Owner”:“user1@front”,
:
:
},
“YYYYY.pbs”:{
“Job_Name”:nan,
“Job_Owner”:“user2@front”,
:
:
},
:
:
—/

This is a bug in qstat. It uses JSON_NONE for a value type (i.e., let JSON encoder decide), where it should use JSON_STRING for values that must be strings.

The following patch works in limited testing, but needs more soak time before I submit it. Compile with -DNASXXX to enable the patch.

There are more attributes that might cause trouble, but only if the admin picks bad names for users, groups, etc.

diff --git a/src/cmds/qstat.c b/src/cmds/qstat.c
index 79d7a67a..6b0f08a4 100644
--- a/src/cmds/qstat.c
+++ b/src/cmds/qstat.c
@@ -411,6 +411,25 @@ prt_attr(char *name, char *resource, char *value, int one_line) {
 		return;
 	switch (output_format) {
 	case FORMAT_JSON:
+#ifdef	NASXXX /* localmod XXX23 */
+		/* Terminate any active resource list */
+		if (prev_resc_name && resource == NULL) {
+			if (add_json_node(JSON_OBJECT_END, JSON_NULL, JSON_NOVALUE, NULL, NULL)
+					== NULL)
+				exit_qstat("out of memory");
+			prev_resc_name = NULL;
+		}
+		/* Handle attributes whose values must be strings, but might be
+		 * confused for numbers, etc.
+		 */
+		if (strcmp(name, ATTR_name) == 0 ||
+		    strcmp(name, ATTR_M) == 0 ||
+		    strcmp(name, ATTR_project) == 0 ||
+		    strcmp(name, ATTR_comment) == 0 ) {
+			if (add_json_node(JSON_VALUE, JSON_STRING, JSON_ESCAPE, name, value) == NULL)
+				exit_qstat("out of memory");
+		} else
+#endif /* localmod XXX23 */
 		if (strcmp(name, ATTR_v) == 0) {
 			if (add_json_node(JSON_OBJECT, JSON_NULL, JSON_NOVALUE, name, NULL) == NULL)
 				exit_qstat("out of memory");
@@ -435,7 +454,11 @@ prt_attr(char *name, char *resource, char *value, int one_line) {
 					*buf++ = *value++;
 				}
 				*buf = '\0';
+#ifdef NASXXX /* localmod XXX22 */
+				if (add_json_node(JSON_VALUE, JSON_STRING, JSON_ESCAPE, key, val) == NULL)
+#else
 				if (add_json_node(JSON_VALUE, JSON_NULL, JSON_ESCAPE, key, val) == NULL)
+#endif /* localmod XXX22 */
 					exit_qstat("out of memory");
 				if (*value != '\0')
 					value++;
@@ -461,12 +484,14 @@ prt_attr(char *name, char *resource, char *value, int one_line) {
 						== NULL)
 					exit_qstat("out of memory");
 			} else {
+#ifndef NASXXX /* localmod XXX23 */
 				if (prev_resc_name) {
 					if (add_json_node(JSON_OBJECT_END, JSON_NULL, JSON_NOVALUE, NULL, NULL)
 							== NULL)
 						exit_qstat("out of memory");
 					prev_resc_name = NULL;
 				}
+#endif /* localmod XXX23 */
 				if (add_json_node(JSON_VALUE, JSON_NULL, JSON_ESCAPE, name, value) == NULL)
 					exit_qstat("out of memory");
 			}

Thank you for your support.
I want to compile qstat.c + patch(JSON_STRING).

Could you tell me about the following ?

  1. How to get the sources and patch(JSON_STRING).

  2. how to compile qstat.c.

This could be a challenge if you are not used to building code from the 'Net. Try the first steps as yourself (not root):

git clone https://github.com/openpbs/openpbs.git
cd openpbs
git checkout -b testing v19.1.1

Examine the INSTALL file you see there. Become root and install the packages needed as described in Steps 1 and 2 for the operating system you are running.

Drop back to yourself (not root). From the INSTALL file, skip step 3 and run steps 4 through 7.

If the make step succeeds, test that an unpatched qstat works, and still has the problem:

src/cmds/qstat -x -f -F JSON

Now, apply the patch. Copy the text of the patch from above and paste it into a patch file:

cat > a.patch
[paste text of patch]

<ctrl-D>

Apply the patch, rerun configure with a new option, and rebuild:

patch -p1 < a.patch
./configure --prefix=/opt/pbs CFLAGS=-DNASXXX
make

Try the new version:

src/cmds/qstat -x -f -F JSON

If this fixes the issue, replace the installed qstat with src/cmds/qstat.