Replace usage of strsep with POSIX strtok_r()

This commit is contained in:
Ondřej Surý
2018-03-21 21:08:29 +00:00
parent b9552250cb
commit 921d05ddcf
17 changed files with 109 additions and 330 deletions

View File

@@ -286,47 +286,24 @@ quit(1);
int
getzone(DB *dbp, const DBT *pkey, const DBT *pdata, DBT *skey) {
char *tmp;
char *left;
char *right;
int result=0;
char *token, *last;
UNUSED(dbp);
UNUSED(pkey);
/* Allocate memory to use in parsing the string */
tmp = right = malloc(pdata->size + 1);
/* verify memory was allocated */
if (right == NULL) {
result = BDBparseErr;
goto getzone_cleanup;
}
/* copy data string into newly allocated memory */
strncpy(right, pdata->data, pdata->size);
right[pdata->size] = '\0';
/* split string at the first space */
left = isc_string_separate(&right, " ");
if ((token = strtok_r(pdata->data, " ", &last)) == NULL) {
return BDBparseErr;
}
/* copy string for "zone" secondary index */
skey->data = strdup(left);
if (skey->data == NULL) {
result = BDBparseErr;
goto getzone_cleanup;
if ((skey->data = strdup(token)) == NULL) {
return BDBparseErr;
}
/* set required values for BDB */
skey->size = strlen(skey->data);
skey->flags = DB_DBT_APPMALLOC;
getzone_cleanup:
/* cleanup memory */
if (tmp != NULL)
free(tmp);
return result;
return 0;
}
/*%
@@ -335,56 +312,30 @@ getzone(DB *dbp, const DBT *pkey, const DBT *pdata, DBT *skey) {
int
gethost(DB *dbp, const DBT *pkey, const DBT *pdata, DBT *skey) {
char *tmp;
char *left;
char *right;
int result=0;
char *token, *last;
UNUSED(dbp);
UNUSED(pkey);
/* allocate memory to use in parsing the string */
tmp = right = malloc(pdata->size + 1);
/* verify memory was allocated */
if (tmp == NULL) {
result = BDBparseErr;
goto gethost_cleanup;
}
/* copy data string into newly allocated memory */
strncpy(right, pdata->data, pdata->size);
right[pdata->size] = '\0';
/* we don't care about left string. */
/* memory of left string will be freed when tmp is freed. */
isc_string_separate(&right, " ");
/* verify right still has some characters left */
if (right == NULL) {
result = BDBparseErr;
goto gethost_cleanup;
/* we don't care about first token. */
if ((token = strtok_r(right, " ", &last)) == NULL) {
return BDBparseErr;
}
/* get "host" from data string */
left = isc_string_separate(&right, " ");
if ((token = strtok_r(NULL, " ", &last)) == NULL) {
return BDBparseErr;
}
/* copy string for "host" secondary index */
skey->data = strdup(left);
if (skey->data == NULL) {
result = BDBparseErr;
goto gethost_cleanup;
if ((skey->data = strdup(token)) == NULL) {
return BDBparseErr;
}
/* set required values for BDB */
skey->size = strlen(skey->data);
skey->flags = DB_DBT_APPMALLOC;
gethost_cleanup:
/* cleanup memory */
if (tmp != NULL)
free(tmp);
return result;
return 0;
}
/*%

View File

@@ -158,9 +158,9 @@ build_querylist(isc_mem_t *mctx, const char *query_str, char **zone,
* split string at the first "$". set query segment to
* left portion
*/
char *last = NULL;
tseg->sql = isc_mem_strdup(mctx,
isc_string_separate(&right_str,
"$"));
strtok_r(right_str, "$", &last));
if (tseg->sql == NULL) {
/* no memory, clean everything up. */
result = ISC_R_NOMEMORY;

View File

@@ -132,14 +132,16 @@ build_querylist(const char *query_str, char **zone, char **record,
}
/* loop through the string and chop it up */
while (right_str != NULL) {
for (token = strtok_r(right_str, "$", &temp_str);
token;
token = strtok_r(NULL, "$", &temp_str))
{
/* allocate memory for tseg */
tseg = calloc(1, sizeof(query_segment_t));
if (tseg == NULL) { /* no memory, clean everything up. */
result = ISC_R_NOMEMORY;
goto cleanup;
}
tseg->cmd = NULL;
tseg->direct = ISC_FALSE;
/* initialize the query segment link */
DLZ_LINK_INIT(tseg, link);
@@ -150,7 +152,7 @@ build_querylist(const char *query_str, char **zone, char **record,
* split string at the first "$". set query segment to
* left portion
*/
tseg->cmd = strdup(strsep(&right_str, "$"));
tseg->cmd = strdup(token);
if (tseg->cmd == NULL) {
/* no memory, clean everything up. */
result = ISC_R_NOMEMORY;
@@ -203,7 +205,8 @@ build_querylist(const char *query_str, char **zone, char **record,
}
/* we don't need temp_str any more */
free(temp_str);
free(right_str);
right_str = NULL;
/*
* add checks later to verify zone and record are found if
* necessary.
@@ -246,8 +249,9 @@ build_querylist(const char *query_str, char **zone, char **record,
cleanup:
/* get rid of temp_str */
if (temp_str != NULL)
free(temp_str);
if (right_str != NULL) {
free(right_str);
}
flag_fail:
/* get rid of what was build of the query list */