[automerger skipped] external: tinycompress: add support for compress plugins am: 2b8db68d31 am: 93ef2fc37e am: b2de43cbb5 -s ours

am skip reason: Change-Id I71f588b9ed84b62d3b1b08dae7ce65f8fa649e6a with SHA-1 931e7cf24b is in history

Original change: https://android-review.googlesource.com/c/platform/external/tinycompress/+/1551054

MUST ONLY BE SUBMITTED BY AUTOMERGER

Change-Id: I712fee58aafd59eff290c5a07b702eb051c31191
diff --git a/compress_plugin.c b/compress_plugin.c
index d445ae9..7a5538d 100644
--- a/compress_plugin.c
+++ b/compress_plugin.c
@@ -1,6 +1,6 @@
 /* compress_plugin.c
 **
-** Copyright (c) 2019, The Linux Foundation. All rights reserved.
+** Copyright (c) 2019-2020, The Linux Foundation. All rights reserved.
 **
 ** Redistribution and use in source and binary forms, with or without
 ** modification, are permitted provided that the following conditions are
@@ -333,10 +333,9 @@
 			unsigned int flags, void **data, void *node)
 {
 	struct compress_plug_data *plug_data;
-	const char *err = NULL;
 	void *dl_hdl;
 	int rc = 0;
-	char *so_name, *open_fn, token[80], *name;
+	char *so_name, *open_fn, token[80], *name, *token_saveptr;
 
 	plug_data = calloc(1, sizeof(*plug_data));
 	if (!plug_data) {
@@ -361,22 +360,26 @@
 	}
 
 	sscanf(so_name, "lib%s", token);
-	name = strtok(token, ".");
-	open_fn = calloc(1, strlen(name) + strlen("_open") + 1);
+	token_saveptr = token;
+	name = strtok_r(token, ".", &token_saveptr);
+	if (!name) {
+		fprintf(stderr, "%s: invalid library name\n", __func__);
+		goto err_open_fn;
+	}
+	const size_t open_fn_size = strlen(name) + strlen("_open") + 1;
+	open_fn = calloc(1, open_fn_size);
 	if (!open_fn) {
 		rc = -ENOMEM;
 		goto err_open_fn;
 	}
 
-	strncpy(open_fn, name, strlen(name) + 1);
-	strcat(open_fn, "_open");
+	strlcpy(open_fn, name, open_fn_size);
+	strlcat(open_fn, "_open", open_fn_size);
 
 	plug_data->plugin_open_fn = dlsym(dl_hdl, open_fn);
-	err = dlerror();
-
-	if (err) {
+	if (!plug_data->plugin_open_fn) {
 		fprintf(stderr, "%s: dlsym to open fn failed, err = '%s'\n",
-				__func__, err);
+				__func__, dlerror());
 		goto err_dlsym;
 	}