[Git][NTPsec/ntpsec][master] 3 commits: refclock_gpsd: Shorten over long lines. No functional changes.

Gary E. Miller gitlab at mg.gitlab.com
Tue Apr 2 03:01:27 UTC 2019



Gary E. Miller pushed to branch master at NTPsec / ntpsec


Commits:
72ad5795 by Gary E. Miller at 2019-04-02T01:50:09Z
refclock_gpsd: Shorten over long lines.  No functional changes.

- - - - -
ffa95f7a by Gary E. Miller at 2019-04-02T02:50:41Z
refclock_gpsd: Tweak some loops to prevent runaway.

Should be no funcional change.  Test for in the buffer, as opposed to
testing for the end of the buffer.

- - - - -
a94224fd by Gary E. Miller at 2019-04-02T03:00:50Z
libjsmn: Update our copy to latest from github.

- - - - -


6 changed files:

- libjsmn/README.md
- libjsmn/example/jsondump.c
- libjsmn/example/simple.c
- libjsmn/jsmn.c
- libjsmn/jsmn.h
- ntpd/refclock_gpsd.c


Changes:

=====================================
libjsmn/README.md
=====================================
@@ -1,16 +1,17 @@
-
 JSMN
 ====
 
+[![Build Status](https://travis-ci.org/zserge/jsmn.svg?branch=master)](https://travis-ci.org/zserge/jsmn)
+
 jsmn (pronounced like 'jasmine') is a minimalistic JSON parser in C.  It can be
 easily integrated into resource-limited or embedded projects.
 
 You can find more information about JSON format at [json.org][1]
 
-Library sources are available at [bitbucket.org/zserge/jsmn][2]
+Library sources are available at https://github.com/zserge/jsmn
 
 The web page with some information about jsmn can be found at
-[http://zserge.com/jsmn.html][3]
+[http://zserge.com/jsmn.html][2]
 
 Philosophy
 ----------
@@ -24,7 +25,7 @@ JSON format itself is extremely simple, so why should we complicate it?
 
 jsmn is designed to be	**robust** (it should work fine even with erroneous
 data), **fast** (it should parse data on the fly), **portable** (no superfluous
-dependencies or non-standard C extensions). An of course, **simplicity** is a
+dependencies or non-standard C extensions). And of course, **simplicity** is a
 key feature - simple code style, simple algorithm, simple integration into
 other projects.
 
@@ -78,9 +79,9 @@ it possible to use zero-copy techniques.
 Install
 -------
 
-To clone the repository you should have mercurial installed. Just run:
+To clone the repository you should have Git installed. Just run:
 
-	$ hg clone http://bitbucket.org/zserge/jsmn jsmn
+	$ git clone https://github.com/zserge/jsmn
 
 Repository layout is simple: jsmn.c and jsmn.h are library files, tests are in
 the jsmn\_test.c, you will also find README, LICENSE and Makefile files inside.
@@ -97,10 +98,11 @@ API
 Token types are described by `jsmntype_t`:
 
 	typedef enum {
-		JSMN_PRIMITIVE = 0,
+		JSMN_UNDEFINED = 0,
 		JSMN_OBJECT = 1,
 		JSMN_ARRAY = 2,
-		JSMN_STRING = 3
+		JSMN_STRING = 3,
+		JSMN_PRIMITIVE = 4
 	} jsmntype_t;
 
 **Note:** Unlike JSON data types, primitive tokens are not divided into
@@ -134,12 +136,12 @@ All job is done by `jsmn_parser` object. You can initialize a new parser using:
 	// js - pointer to JSON string
 	// tokens - an array of tokens available
 	// 10 - number of tokens available
-	jsmn_parse(&parser, js, tokens, 10);
+	jsmn_parse(&parser, js, strlen(js), tokens, 10);
 
 This will create a parser, and then it tries to parse up to 10 JSON tokens from
 the `js` string.
 
-A non-negative reutrn value of `jsmn_parse` is the number of tokens actually
+A non-negative return value of `jsmn_parse` is the number of tokens actually
 used by the parser.
 Passing NULL instead of the tokens array would not store parsing results, but
 instead the function will return the value of tokens needed to parse the given
@@ -151,9 +153,9 @@ If something goes wrong, you will get an error. Error will be one of these:
 * `JSMN_ERROR_NOMEM` - not enough tokens, JSON string is too large
 * `JSMN_ERROR_PART` - JSON string is too short, expecting more JSON data
 
-If you get `JSON_ERROR_NOMEM`, you can re-allocate more tokens and call
+If you get `JSMN_ERROR_NOMEM`, you can re-allocate more tokens and call
 `jsmn_parse` once more.  If you read json data from the stream, you can
-periodically call `jsmn_parse` and check if return value is `JSON_ERROR_PART`.
+periodically call `jsmn_parse` and check if return value is `JSMN_ERROR_PART`.
 You will get this error until you reach the end of JSON data.
 
 Other info
@@ -163,5 +165,4 @@ This software is distributed under [MIT license](http://www.opensource.org/licen
  so feel free to integrate it in your commercial products.
 
 [1]: http://www.json.org/
-[2]: https://bitbucket.org/zserge/jsmn/wiki/Home
-[3]: http://zserge.com/jsmn.html
+[2]: http://zserge.com/jsmn.html


=====================================
libjsmn/example/jsondump.c
=====================================
@@ -1,9 +1,25 @@
 #include <stdio.h>
 #include <stdlib.h>
+#include <unistd.h>
 #include <string.h>
 #include <errno.h>
 #include "../jsmn.h"
 
+/* Function realloc_it() is a wrapper function for standard realloc()
+ * with one difference - it frees old memory pointer in case of realloc
+ * failure. Thus, DO NOT use old data pointer in anyway after call to
+ * realloc_it(). If your code has some kind of fallback algorithm if
+ * memory can't be re-allocated - use standard realloc() instead.
+ */
+static inline void *realloc_it(void *ptrmem, size_t size) {
+	void *p = realloc(ptrmem, size);
+	if (!p)  {
+		free (ptrmem);
+		fprintf(stderr, "realloc(): errno=%d\n", errno);
+	}
+	return p;
+}
+
 /*
  * An example of reading JSON from stdin and printing its content to stdout.
  * The output looks like YAML, but I'm not sure if it's really compatible.
@@ -82,12 +98,11 @@ int main() {
 			}
 		}
 
-		js = realloc(js, jslen + r + 1);
+		js = realloc_it(js, jslen + r + 1);
 		if (js == NULL) {
-			fprintf(stderr, "realloc(): errno=%d\n", errno);
 			return 3;
 		}
-		strlcpy(js + jslen, buf, r + 1);
+		strncpy(js + jslen, buf, r);
 		jslen = jslen + r;
 
 again:
@@ -95,9 +110,8 @@ again:
 		if (r < 0) {
 			if (r == JSMN_ERROR_NOMEM) {
 				tokcount = tokcount * 2;
-				tok = realloc(tok, sizeof(*tok) * tokcount);
+				tok = realloc_it(tok, sizeof(*tok) * tokcount);
 				if (tok == NULL) {
-					fprintf(stderr, "realloc(): errno=%d\n", errno);
 					return 3;
 				}
 				goto again;
@@ -108,5 +122,5 @@ again:
 		}
 	}
 
-	return 0;
+	return EXIT_SUCCESS;
 }


=====================================
libjsmn/example/simple.c
=====================================
@@ -1,4 +1,5 @@
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 #include "../jsmn.h"
 
@@ -7,7 +8,7 @@
  * tokens is predictable.
  */
 
-const char *JSON_STRING =
+static const char *JSON_STRING =
 	"{\"user\": \"johndoe\", \"admin\": false, \"uid\": 1000,\n  "
 	"\"groups\": [\"users\", \"wheel\", \"audio\", \"video\"]}";
 
@@ -71,5 +72,5 @@ int main() {
 					JSON_STRING + t[i].start);
 		}
 	}
-	return 0;
+	return EXIT_SUCCESS;
 }


=====================================
libjsmn/jsmn.c
=====================================
@@ -1,11 +1,7 @@
-/* SPDX-License-Identifier: MIT */
-
-#include <stdlib.h>
-
 #include "jsmn.h"
 
 /**
- * Allocates a fresh unused token from the token pull.
+ * Allocates a fresh unused token from the token pool.
  */
 static jsmntok_t *jsmn_alloc_token(jsmn_parser *parser,
 		jsmntok_t *tokens, size_t num_tokens) {
@@ -36,28 +32,25 @@ static void jsmn_fill_token(jsmntok_t *token, jsmntype_t type,
 /**
  * Fills next available token with JSON primitive.
  */
-static jsmnerr_t jsmn_parse_primitive(jsmn_parser *parser, const char *js,
+static int jsmn_parse_primitive(jsmn_parser *parser, const char *js,
 		size_t len, jsmntok_t *tokens, size_t num_tokens) {
 	jsmntok_t *token;
 	int start;
 
-	start = (int)parser->pos;
+	start = parser->pos;
 
 	for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
 		switch (js[parser->pos]) {
 #ifndef JSMN_STRICT
-		/* In strict mode primitive must be followed by "," or "}" or "]" */
-		case ':':
+			/* In strict mode primitive must be followed by "," or "}" or "]" */
+			case ':':
 #endif
-		case '\t' : case '\r' : case '\n' : case ' ' :
-		case ','  : case ']'  : case '}' :
-			goto found;
-                default:
-                        /* huh? */
-                        break;
+			case '\t' : case '\r' : case '\n' : case ' ' :
+			case ','  : case ']'  : case '}' :
+				goto found;
 		}
 		if (js[parser->pos] < 32 || js[parser->pos] >= 127) {
-			parser->pos = (unsigned int)start;
+			parser->pos = start;
 			return JSMN_ERROR_INVAL;
 		}
 	}
@@ -74,10 +67,10 @@ found:
 	}
 	token = jsmn_alloc_token(parser, tokens, num_tokens);
 	if (token == NULL) {
-		parser->pos = (unsigned int)start;
+		parser->pos = start;
 		return JSMN_ERROR_NOMEM;
 	}
-	jsmn_fill_token(token, JSMN_PRIMITIVE, start, (int)parser->pos);
+	jsmn_fill_token(token, JSMN_PRIMITIVE, start, parser->pos);
 #ifdef JSMN_PARENT_LINKS
 	token->parent = parser->toksuper;
 #endif
@@ -86,13 +79,13 @@ found:
 }
 
 /**
- * Filsl next token with JSON string.
+ * Fills next token with JSON string.
  */
-static jsmnerr_t jsmn_parse_string(jsmn_parser *parser, const char *js,
+static int jsmn_parse_string(jsmn_parser *parser, const char *js,
 		size_t len, jsmntok_t *tokens, size_t num_tokens) {
 	jsmntok_t *token;
 
-	int start = (int)parser->pos;
+	int start = parser->pos;
 
 	parser->pos++;
 
@@ -107,11 +100,10 @@ static jsmnerr_t jsmn_parse_string(jsmn_parser *parser, const char *js,
 			}
 			token = jsmn_alloc_token(parser, tokens, num_tokens);
 			if (token == NULL) {
-				parser->pos = (unsigned int)start;
+				parser->pos = start;
 				return JSMN_ERROR_NOMEM;
 			}
-			jsmn_fill_token(token, JSMN_STRING, start+1,
-                                        (int)parser->pos);
+			jsmn_fill_token(token, JSMN_STRING, start+1, parser->pos);
 #ifdef JSMN_PARENT_LINKS
 			token->parent = parser->toksuper;
 #endif
@@ -131,37 +123,37 @@ static jsmnerr_t jsmn_parse_string(jsmn_parser *parser, const char *js,
 				case 'u':
 					parser->pos++;
 					for(i = 0; i < 4 && parser->pos < len && js[parser->pos] != '\0'; i++) {
-					    /* If it isn't a hex character we have an error */
-					    if(!((js[parser->pos] >= 48 && js[parser->pos] <= 57) || /* 0-9 */
-								    (js[parser->pos] >= 65 && js[parser->pos] <= 70) || /* A-F */
-								    (js[parser->pos] >= 97 && js[parser->pos] <= 102))) { /* a-f */
-						parser->pos = (unsigned int)start;
-						return JSMN_ERROR_INVAL;
-					    }
-					    parser->pos++;
+						/* If it isn't a hex character we have an error */
+						if(!((js[parser->pos] >= 48 && js[parser->pos] <= 57) || /* 0-9 */
+									(js[parser->pos] >= 65 && js[parser->pos] <= 70) || /* A-F */
+									(js[parser->pos] >= 97 && js[parser->pos] <= 102))) { /* a-f */
+							parser->pos = start;
+							return JSMN_ERROR_INVAL;
+						}
+						parser->pos++;
 					}
 					parser->pos--;
 					break;
 				/* Unexpected symbol */
 				default:
-					parser->pos = (unsigned int)start;
+					parser->pos = start;
 					return JSMN_ERROR_INVAL;
 			}
 		}
 	}
-	parser->pos = (unsigned int)start;
+	parser->pos = start;
 	return JSMN_ERROR_PART;
 }
 
 /**
  * Parse JSON string and fill tokens.
  */
-jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, size_t len,
+int jsmn_parse(jsmn_parser *parser, const char *js, size_t len,
 		jsmntok_t *tokens, unsigned int num_tokens) {
-	jsmnerr_t r;
+	int r;
 	int i;
 	jsmntok_t *token;
-	int count = 0;
+	int count = parser->toknext;
 
 	for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
 		char c;
@@ -184,8 +176,8 @@ jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, size_t len,
 #endif
 				}
 				token->type = (c == '{' ? JSMN_OBJECT : JSMN_ARRAY);
-				token->start = (int)parser->pos;
-				parser->toksuper = (int)parser->toknext - 1;
+				token->start = parser->pos;
+				parser->toksuper = parser->toknext - 1;
 				break;
 			case '}': case ']':
 				if (tokens == NULL)
@@ -198,14 +190,17 @@ jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, size_t len,
 				token = &tokens[parser->toknext - 1];
 				for (;;) {
 					if (token->start != -1 && token->end == -1) {
-					    if (token->type != type) {
-						    return JSMN_ERROR_INVAL;
-					    }
-					    token->end = (int)parser->pos + 1;
-					    parser->toksuper = token->parent;
-					    break;
+						if (token->type != type) {
+							return JSMN_ERROR_INVAL;
+						}
+						token->end = parser->pos + 1;
+						parser->toksuper = token->parent;
+						break;
 					}
 					if (token->parent == -1) {
+						if(token->type != type || parser->toksuper == -1) {
+							return JSMN_ERROR_INVAL;
+						}
 						break;
 					}
 					token = &tokens[token->parent];
@@ -243,10 +238,10 @@ jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, size_t len,
 			case '\t' : case '\r' : case '\n' : case ' ':
 				break;
 			case ':':
-				parser->toksuper = (int)parser->toknext - 1;
+				parser->toksuper = parser->toknext - 1;
 				break;
 			case ',':
-				if (tokens != NULL &&
+				if (tokens != NULL && parser->toksuper != -1 &&
 						tokens[parser->toksuper].type != JSMN_ARRAY &&
 						tokens[parser->toksuper].type != JSMN_OBJECT) {
 #ifdef JSMN_PARENT_LINKS
@@ -269,7 +264,7 @@ jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, size_t len,
 			case '5': case '6': case '7' : case '8': case '9':
 			case 't': case 'f': case 'n' :
 				/* And they must not be keys of the object */
-				if (tokens != NULL) {
+				if (tokens != NULL && parser->toksuper != -1) {
 					jsmntok_t *t = &tokens[parser->toksuper];
 					if (t->type == JSMN_OBJECT ||
 							(t->type == JSMN_STRING && t->size != 0)) {
@@ -294,8 +289,9 @@ jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, size_t len,
 #endif
 		}
 	}
+
 	if (tokens != NULL) {
-		for (i = (int)parser->toknext - 1; i >= 0; i--) {
+		for (i = parser->toknext - 1; i >= 0; i--) {
 			/* Unmatched opened object or array */
 			if (tokens[i].start != -1 && tokens[i].end == -1) {
 				return JSMN_ERROR_PART;


=====================================
libjsmn/jsmn.h
=====================================
@@ -1,7 +1,5 @@
-/* SPDX-License-Identifier: MIT */
-
-#ifndef GUARD_JSMN_H
-#define GUARD_JSMN_H
+#ifndef __JSMN_H_
+#define __JSMN_H_
 
 #include <stddef.h>
 
@@ -17,26 +15,27 @@ extern "C" {
  * 	o Other primitive: number, boolean (true/false) or null
  */
 typedef enum {
-	JSMN_PRIMITIVE = 0,
+	JSMN_UNDEFINED = 0,
 	JSMN_OBJECT = 1,
 	JSMN_ARRAY = 2,
-	JSMN_STRING = 3
+	JSMN_STRING = 3,
+	JSMN_PRIMITIVE = 4
 } jsmntype_t;
 
-typedef enum {
+enum jsmnerr {
 	/* Not enough tokens were provided */
 	JSMN_ERROR_NOMEM = -1,
 	/* Invalid character inside JSON string */
 	JSMN_ERROR_INVAL = -2,
 	/* The string is not a full JSON packet, more bytes expected */
 	JSMN_ERROR_PART = -3
-} jsmnerr_t;
+};
 
 /**
  * JSON token description.
- * @param		type	type (object, array, string etc.)
- * @param		start	start position in JSON data string
- * @param		end		end position in JSON data string
+ * type		type (object, array, string etc.)
+ * start	start position in JSON data string
+ * end		end position in JSON data string
  */
 typedef struct {
 	jsmntype_t type;
@@ -67,11 +66,11 @@ void jsmn_init(jsmn_parser *parser);
  * Run JSON parser. It parses a JSON data string into and array of tokens, each describing
  * a single JSON object.
  */
-jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, size_t len,
+int jsmn_parse(jsmn_parser *parser, const char *js, size_t len,
 		jsmntok_t *tokens, unsigned int num_tokens);
 
 #ifdef __cplusplus
 }
 #endif
 
-#endif /* GUARD_JSMN_H */
+#endif /* __JSMN_H_ */


=====================================
ntpd/refclock_gpsd.c
=====================================
@@ -519,12 +519,14 @@ gpsd_start(
                 }
 		if (-1 == ret ) {
                         /* more likely out of RAM */
-			msyslog(LOG_ERR, "REFCLOCK: %s: clock device name too long",
+			msyslog(LOG_ERR,
+                                "REFCLOCK: %s: clock device name too long",
 				up->logname);
 			goto dev_fail;
 		}
 		if (-1 == stat(up->device, &sb) || !S_ISCHR(sb.st_mode)) {
-			msyslog(LOG_ERR, "REFCLOCK: %s: '%s' is not a character device",
+			msyslog(LOG_ERR,
+                                "REFCLOCK: %s: '%s' is not a character device",
 				up->logname, up->device);
 			goto dev_fail;
 		}
@@ -554,7 +556,8 @@ gpsd_start(
 
 	/* If the daemon name lookup failed, just give up now. */
 	if (NULL == up->addr) {
-		msyslog(LOG_ERR, "REFCLOCK: %s: no GPSD socket address, giving up",
+		msyslog(LOG_ERR,
+			"REFCLOCK: %s: no GPSD socket address, giving up",
 			up->logname);
 		goto dev_fail;
 	}
@@ -661,8 +664,7 @@ gpsd_receive(
 	pdst = up->buffer + up->buflen;
 	edst = pdst + sizeof(up->buffer) - 1; /* for trailing NUL */
 
-        /* FIXME!! Check for overrun of "buffer" */
-	while (psrc != esrc) {
+	while (psrc < esrc) {
 		ch = *psrc++;
 		if (ch == '\n') {
 			/* trim trailing whitespace & terminate buffer */
@@ -673,7 +675,7 @@ gpsd_receive(
 			up->buflen = (int)(pdst - up->buffer);
 			gpsd_parse(peer, &rbufp->recv_time);
 			pdst = up->buffer;
-		} else if (pdst != edst) {
+		} else if (pdst < edst) {
 			/* add next char, ignoring leading whitespace */
 			if (ch > ' ' || pdst != up->buffer)
 				*pdst++ = ch;
@@ -1446,7 +1448,8 @@ process_version(
 	if (0 == errno) {
 		if ( ! up->fl_vers)
 			msyslog(LOG_INFO,
-				"REFCLOCK: %s: GPSD revision=%s release=%s protocol=%u.%u",
+				"REFCLOCK: %s: GPSD revision=%s release=%s "
+				"protocol=%u.%u",
 				up->logname, revision, release,
 				pvhi, pvlo);
 		up->proto_version = PROTO_VERSION(pvhi, pvlo);
@@ -1495,7 +1498,8 @@ process_version(
 		 * resulting data timeout will take care of the
 		 * connection!
 		 */
-		msyslog(LOG_ERR, "REFCLOCK: %s: failed to write watch request (%s)",
+		msyslog(LOG_ERR,
+                        "REFCLOCK: %s: failed to write watch request (%s)",
 			up->logname, strerror(errno));
 	}
 }
@@ -1853,7 +1857,8 @@ gpsd_init_socket(
 	if (-1 == rc) {
 		if (syslogok(pp, up))
 			msyslog(LOG_ERR,
-				"REFCLOCK: %s: cannot set GPSD socket to non-blocking: %s",
+				"REFCLOCK: %s: cannot set GPSD socket "
+                                "to non-blocking: %s",
 				up->logname, strerror(errno));
 		goto no_socket;
 	}
@@ -1904,7 +1909,8 @@ gpsd_init_socket(
 	if (0 == io_addclock(&pp->io)) {
 		if (syslogok(pp, up))
 			msyslog(LOG_ERR,
-				"REFCLOCK: %s: failed to register with I/O engine",
+				"REFCLOCK: %s: failed to register "
+                                "with I/O engine",
 				up->logname);
 		goto no_socket;
 	}
@@ -1986,8 +1992,8 @@ gpsd_test_socket(
 	if (0 == io_addclock(&pp->io)) {
 		if (syslogok(pp, up))
 			msyslog(LOG_ERR,
-				"REFCLOCK: %s: failed to register with I/O engine",
-				up->logname);
+			    "REFCLOCK: %s: failed to register with I/O engine",
+			    up->logname);
 		goto no_socket;
 	}
 	return;



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/bd89f0dd230643e0fd689f1075e1e8f367d81061...a94224fdd4cae05e983dd0b40f3edbff9102b85e

-- 
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/bd89f0dd230643e0fd689f1075e1e8f367d81061...a94224fdd4cae05e983dd0b40f3edbff9102b85e
You're receiving this email because of your account on gitlab.com.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20190402/e2456304/attachment-0001.html>


More information about the vc mailing list