Skip to content

Commit 5d9a463

Browse files
authored
Replace strtok in systemd-sonic-generator (#11710)
Signed-off-by: maipbui <[email protected]> <!-- Please make sure you've read and understood our contributing guidelines: https://github.com/Azure/SONiC/blob/gh-pages/CONTRIBUTING.md ** Make sure all your commits include a signature generated with `git commit -s` ** If this is a bug fix, make sure your description includes "fixes #xxxx", or "closes #xxxx" or "resolves #xxxx" Please provide the following information: --> #### Why I did it Replace unsafe functions to safe functions #### How I did it Replace `strtok()` by `strtok_r()` #### How to verify it #### Which release branch to backport (provide reason below if selected) <!-- - Note we only backport fixes to a release branch, *not* features! - Please also provide a reason for the backporting below. - e.g. - [x] 202006 --> - [ ] 201811 - [ ] 201911 - [ ] 202006 - [ ] 202012 - [ ] 202106 - [ ] 202111 - [ ] 202205 #### Description for the changelog <!-- Write a short (one line) summary that describes the changes in this pull request for inclusion in the changelog: --> #### Link to config_db schema for YANG module changes <!-- Provide a link to config_db schema for the table for which YANG model is defined Link should point to correct section on https://github.com/Azure/sonic-buildimage/blob/master/src/sonic-yang-models/doc/Configuration.md --> #### A picture of a cute animal (not mandatory but encouraged)
1 parent 535612f commit 5d9a463

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

src/systemd-sonic-generator/systemd-sonic-generator.c

+11-8
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ static int get_install_targets_from_line(char* target_string, char* install_type
121121
***/
122122
char* token;
123123
char* target;
124+
char* saveptr;
124125
char final_target[PATH_MAX];
125126
int num_targets = 0;
126127

@@ -135,8 +136,8 @@ static int get_install_targets_from_line(char* target_string, char* install_type
135136
strip_trailing_newline(target);
136137

137138
if (strstr(target, "%") != NULL) {
138-
char* prefix = strtok(target, ".");
139-
char* suffix = strtok(NULL, ".");
139+
char* prefix = strtok_r(target, ".", &saveptr);
140+
char* suffix = strtok_r(NULL, ".", &saveptr);
140141
int prefix_len = strlen(prefix);
141142

142143
strncpy(final_target, prefix, prefix_len - 2);
@@ -516,6 +517,7 @@ int get_num_of_asic() {
516517
char *line = NULL;
517518
char* token;
518519
char* platform;
520+
char* saveptr;
519521
size_t len = 0;
520522
ssize_t nread;
521523
bool ans;
@@ -534,8 +536,8 @@ int get_num_of_asic() {
534536
while ((nread = getline(&line, &len, fp)) != -1) {
535537
if ((strstr(line, "onie_platform") != NULL) ||
536538
(strstr(line, "aboot_platform") != NULL)) {
537-
token = strtok(line, "=");
538-
platform = strtok(NULL, "=");
539+
token = strtok_r(line, "=", &saveptr);
540+
platform = strtok_r(NULL, "=", &saveptr);
539541
strip_trailing_newline(platform);
540542
break;
541543
}
@@ -547,8 +549,8 @@ int get_num_of_asic() {
547549
if (fp != NULL) {
548550
while ((nread = getline(&line, &len, fp)) != -1) {
549551
if (strstr(line, "NUM_ASIC") != NULL) {
550-
token = strtok(line, "=");
551-
str_num_asic = strtok(NULL, "=");
552+
token = strtok_r(line, "=", &saveptr);
553+
str_num_asic = strtok_r(NULL, "=", &saveptr);
552554
strip_trailing_newline(str_num_asic);
553555
if (str_num_asic != NULL){
554556
sscanf(str_num_asic, "%d",&num_asic);
@@ -571,6 +573,7 @@ int ssg_main(int argc, char **argv) {
571573
char* unit_instance;
572574
char* prefix;
573575
char* suffix;
576+
char* saveptr;
574577
int num_unit_files;
575578
int num_targets;
576579
int r;
@@ -589,8 +592,8 @@ int ssg_main(int argc, char **argv) {
589592
for (int i = 0; i < num_unit_files; i++) {
590593
unit_instance = strdup(unit_files[i]);
591594
if ((num_asics == 1) && strstr(unit_instance, "@") != NULL) {
592-
prefix = strtok(unit_instance, "@");
593-
suffix = strtok(NULL, "@");
595+
prefix = strtok_r(unit_instance, "@", &saveptr);
596+
suffix = strtok_r(NULL, "@", &saveptr);
594597

595598
strcpy(unit_instance, prefix);
596599
strcat(unit_instance, suffix);

0 commit comments

Comments
 (0)