Skip to content

Commit

Permalink
[systemd-sonic-generator] Fix overlapping strings being passed to str…
Browse files Browse the repository at this point in the history
…cpy/strcat (#13647)

#### Why I did it

Fix an issue that services do not start automatically on first boot and start only after hostcfgd enables them.
This is due to a bug in systemd-sonic-generator:

```
admin@arc-switch1004:~$ /usr/lib/systemd/system-generators/systemd-sonic-generator dir
Failed to open file /usr/lib/systemd/system/database.servcee
Error parsing targets for database.servcee
Error parsing database.servcee
Failed to open file /usr/lib/systemd/system/bgp.servcee
Error parsing targets for bgp.servcee
Error parsing bgp.servcee
Failed to open file /usr/lib/systemd/system/lldp.servcee
Error parsing targets for lldp.servcee
Error parsing lldp.servcee
Failed to open file /usr/lib/systemd/system/swss.servcee
Error parsing targets for swss.servcee
Error parsing swss.servcee
Failed to open file /usr/lib/systemd/system/teamd.servcee
Error parsing targets for teamd.servcee
Error parsing teamd.servcee
Failed to open file /usr/lib/systemd/system/syncd.servcee
Error parsing targets for syncd.servcee
Error parsing syncd.servcee
```

A wrong file name is generated (e.g database.**servcee**).

#### How I did it

Fixed overlapping strings being passed to strcpy/strcat that receive restirct* pointers (strings should not overlap).

#### How to verify it

Perform first boot and observe services start immidiatelly after boot.
  • Loading branch information
stepanblyschak authored and mssonicbld committed Feb 22, 2023
1 parent 602ffb1 commit b51de79
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/systemd-sonic-generator/systemd-sonic-generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -608,11 +608,14 @@ int ssg_main(int argc, char **argv) {
for (int i = 0; i < num_unit_files; i++) {
unit_instance = strdup(unit_files[i]);
if ((num_asics == 1) && strstr(unit_instance, "@") != NULL) {
prefix = strtok_r(unit_instance, "@", &saveptr);
suffix = strtok_r(NULL, "@", &saveptr);
prefix = strdup(strtok_r(unit_instance, "@", &saveptr));
suffix = strdup(strtok_r(NULL, "@", &saveptr));

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

free(prefix);
free(suffix);
}

num_targets = get_install_targets(unit_instance, targets);
Expand Down

0 comments on commit b51de79

Please sign in to comment.