Merge lp:~jamesodhunt/upstart/upstart-file-bridge-tidyup into lp:upstart

Proposed by James Hunt
Status: Merged
Merged at revision: 1455
Proposed branch: lp:~jamesodhunt/upstart/upstart-file-bridge-tidyup
Merge into: lp:upstart
Diff against target: 236 lines (+46/-41)
2 files modified
ChangeLog (+12/-0)
extra/upstart-file-bridge.c (+34/-41)
To merge this branch: bzr merge lp:~jamesodhunt/upstart/upstart-file-bridge-tidyup
Reviewer Review Type Date Requested Status
Steve Langasek Approve
Review via email: mp+153797@code.launchpad.net

Description of the change

* extra/upstart-file-bridge.c:
  - main(): String safety for home_dir.
  - job_add_file():
    - Initialise events.
    - Use nih_strdup() rather than arrays for paths.
    - Removed unecessary error label.
  - {create_handler,modify_handler,delete_handler}(): Remove strcpy().
  - watched_dir_new(): Use nih_strdup() rather than arrays for path.
  - find_first_parent(): Replace strcpy with strncpy().

To post a comment you must log in.
Revision history for this message
Steve Langasek (vorlon) wrote :

@@ -1683,7 +1675,8 @@

        do {
                /* save parent for next time through the loop */
- strcpy (tmp, current);
+ memset (tmp, '\0', sizeof (tmp));
+ strncpy (tmp, current, sizeof (tmp)-1);
                parent = dirname (tmp);

                /* Ensure dirname returned something sane */

I've always preferred tmp[sizeof (tmp) - 1] = '\0' rather than memset, avoids wasting cycles zero-filling an area that you're just going to overwrite immediately afterwards. But looks good, landing.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'ChangeLog'
--- ChangeLog 2013-03-15 12:51:25 +0000
+++ ChangeLog 2013-03-18 13:40:33 +0000
@@ -1,5 +1,17 @@
12013-03-15 James Hunt <james.hunt@ubuntu.com>12013-03-15 James Hunt <james.hunt@ubuntu.com>
22
3 * extra/upstart-file-bridge.c:
4 - main(): String safety for home_dir.
5 - job_add_file():
6 - Initialise events.
7 - Use nih_strdup() rather than arrays for paths.
8 - Removed unecessary error label.
9 - {create_handler,modify_handler,delete_handler}(): Remove strcpy().
10 - watched_dir_new(): Use nih_strdup() rather than arrays for path.
11 - find_first_parent(): Replace strcpy with strncpy().
12
132013-03-15 James Hunt <james.hunt@ubuntu.com>
14
3 * extra/man/file-event.7: Simplify language.15 * extra/man/file-event.7: Simplify language.
4 * extra/upstart-file-bridge.c:16 * extra/upstart-file-bridge.c:
5 - skip_slashes(): New macro to make path matching more reliable.17 - skip_slashes(): New macro to make path matching more reliable.
618
=== modified file 'extra/upstart-file-bridge.c'
--- extra/upstart-file-bridge.c 2013-03-15 12:44:29 +0000
+++ extra/upstart-file-bridge.c 2013-03-18 13:40:33 +0000
@@ -451,7 +451,8 @@
451451
452 nih_assert (pw->pw_dir);452 nih_assert (pw->pw_dir);
453453
454 strcpy (home_dir, (pw->pw_dir));454 memset (home_dir, '\0', sizeof (home_dir));
455 strncpy (home_dir, pw->pw_dir, sizeof (home_dir)-1);
455 }456 }
456457
457 /* Allocate jobs hash table */458 /* Allocate jobs hash table */
@@ -716,20 +717,18 @@
716job_add_file (Job *job,717job_add_file (Job *job,
717 char **file_info)718 char **file_info)
718{719{
719 uint32_t events;720 uint32_t events = 0x0;
720 WatchedFile *file = NULL;721 WatchedFile *file = NULL;
721 nih_local char *error = NULL;722 nih_local char *error = NULL;
722 nih_local char *glob_expr = NULL;723 nih_local char *glob_expr = NULL;
723 nih_local char *expanded = NULL;724 nih_local char *expanded = NULL;
724 char path[PATH_MAX];725 nih_local char *path = NULL;
725726
726 nih_assert (job);727 nih_assert (job);
727 nih_assert (job->path);728 nih_assert (job->path);
728 nih_assert (file_info);729 nih_assert (file_info);
729 nih_assert (! strcmp (file_info[0], FILE_EVENT));730 nih_assert (! strcmp (file_info[0], FILE_EVENT));
730731
731 memset (path, '\0', sizeof (path));
732
733 for (char **env = file_info + 1; env && *env; env++) {732 for (char **env = file_info + 1; env && *env; env++) {
734 char *val;733 char *val;
735 size_t name_len;734 size_t name_len;
@@ -738,33 +737,33 @@
738 if (! val) {737 if (! val) {
739 nih_warn ("%s: Ignored %s event without variable name",738 nih_warn ("%s: Ignored %s event without variable name",
740 job->path, FILE_EVENT);739 job->path, FILE_EVENT);
741 goto error;740 return;
742 }741 }
743742
744 name_len = val - *env;743 name_len = val - *env;
745 val++;744 val++;
746745
747 if (! strncmp (*env, "FPATH", name_len)) {746 if (! strncmp (*env, "FPATH", name_len)) {
748 char dirpart[PATH_MAX];747 nih_local char *dirpart = NULL;
749 char basepart[PATH_MAX];748 nih_local char *basepart = NULL;
750 char *dir;749 char *dir;
751 char *base;750 char *base;
752 size_t len2;751 size_t len2;
753752
754 strcpy (path, val);753 path = NIH_MUST (nih_strdup (NULL, val));
755754
756 if (user && path[0] != '/') {755 if (user && path[0] != '/') {
757 expanded = expand_path (NULL, path);756 expanded = expand_path (NULL, path);
758 if (! expanded) {757 if (! expanded) {
759 nih_error ("Failed to expand path");758 nih_error ("Failed to expand path");
760 goto error;759 return;
761 }760 }
762 }761 }
763762
764 if (! path_valid (path))763 if (! path_valid (path))
765 goto error;764 return;
766765
767 strcpy (dirpart, path);766 dirpart = NIH_MUST (nih_strdup (NULL, path));
768 dir = dirname (dirpart);767 dir = dirname (dirpart);
769768
770 /* See dirname(3) */769 /* See dirname(3) */
@@ -774,10 +773,10 @@
774773
775 if (strcspn (dir, GLOB_CHARS) < len2) {774 if (strcspn (dir, GLOB_CHARS) < len2) {
776 nih_warn ("%s: %s", job->path, _("Directory globbing not supported"));775 nih_warn ("%s: %s", job->path, _("Directory globbing not supported"));
777 goto error;776 return;
778 }777 }
779778
780 strcpy (basepart, path);779 basepart = NIH_MUST (nih_strdup (NULL, path));
781 base = basename (basepart);780 base = basename (basepart);
782781
783 /* See dirname(3) */782 /* See dirname(3) */
@@ -786,7 +785,8 @@
786 len2 = strlen (base);785 len2 = strlen (base);
787786
788 if (strcspn (base, GLOB_CHARS) < len2) {787 if (strcspn (base, GLOB_CHARS) < len2) {
789 strcpy (path, dir);788 nih_free (path);
789 path = NIH_MUST (nih_strdup (NULL, dir));
790 glob_expr = NIH_MUST (nih_strdup (NULL, base));790 glob_expr = NIH_MUST (nih_strdup (NULL, base));
791 }791 }
792 } else if (! strncmp (*env, "FEVENT", name_len)) {792 } else if (! strncmp (*env, "FEVENT", name_len)) {
@@ -800,8 +800,8 @@
800 }800 }
801 }801 }
802802
803 if (! *path)803 if (! path)
804 goto error;804 return;
805805
806 if (! events)806 if (! events)
807 events = ALL_FILE_EVENTS;807 events = ALL_FILE_EVENTS;
@@ -813,7 +813,7 @@
813 if (! file) {813 if (! file) {
814 nih_warn ("%s: %s",814 nih_warn ("%s: %s",
815 _("Failed to add new file"), path);815 _("Failed to add new file"), path);
816 goto error;816 return;
817 }817 }
818818
819 /* If the job cares about the file or directory existing and it819 /* If the job cares about the file or directory existing and it
@@ -857,12 +857,6 @@
857 }857 }
858858
859 ensure_watched (job, file);859 ensure_watched (job, file);
860
861 return;
862
863error:
864 if (file)
865 nih_free (file);
866}860}
867861
868/**862/**
@@ -966,12 +960,10 @@
966 nih_list_add (&entries, &file->entry);960 nih_list_add (&entries, &file->entry);
967 }961 }
968 } else if (file->glob) {962 } else if (file->glob) {
969 char full_path[PATH_MAX];963 nih_local char *full_path = NULL;
970964
971 /* reconstruct the full path */965 /* reconstruct the full path */
972 strcpy (full_path, file->path);966 full_path = NIH_MUST (nih_sprintf (NULL, "%s/%s", file->path, file->glob));
973 strcat (full_path, "/");
974 strcat (full_path, file->glob);
975967
976 if (! fnmatch (full_path, path, FNM_PATHNAME) && (file->events & IN_CREATE))968 if (! fnmatch (full_path, path, FNM_PATHNAME) && (file->events & IN_CREATE))
977 handle_event (handled, full_path, IN_CREATE, path);969 handle_event (handled, full_path, IN_CREATE, path);
@@ -1082,12 +1074,11 @@
1082 handle_event (handled, original_path (file), IN_MODIFY, path);1074 handle_event (handled, original_path (file), IN_MODIFY, path);
1083 }1075 }
1084 } else if (file->glob) {1076 } else if (file->glob) {
1085 char full_path[PATH_MAX];1077 nih_local char *full_path = NULL;
10861078
1087 /* reconstruct the full path */1079 /* reconstruct the full path */
1088 strcpy (full_path, file->path);1080 full_path = NIH_MUST (nih_sprintf (NULL, "%s/%s", file->path, file->glob));
1089 strcat (full_path, "/");1081
1090 strcat (full_path, file->glob);
1091 if (! fnmatch (full_path, path, FNM_PATHNAME) && (file->events & IN_MODIFY))1082 if (! fnmatch (full_path, path, FNM_PATHNAME) && (file->events & IN_MODIFY))
1092 handle_event (handled, full_path, IN_MODIFY, path);1083 handle_event (handled, full_path, IN_MODIFY, path);
1093 } else {1084 } else {
@@ -1157,12 +1148,10 @@
1157 handle_event (handled, original_path (file), IN_MODIFY, path);1148 handle_event (handled, original_path (file), IN_MODIFY, path);
1158 }1149 }
1159 } else if (file->glob) {1150 } else if (file->glob) {
1160 char full_path[PATH_MAX];1151 nih_local char *full_path = NULL;
11611152
1162 /* reconstruct the full path */1153 /* reconstruct the full path */
1163 strcpy (full_path, file->path);1154 full_path = NIH_MUST (nih_sprintf (NULL, "%s/%s", file->path, file->glob));
1164 strcat (full_path, "/");
1165 strcat (full_path, file->glob);
11661155
1167 if (! fnmatch (full_path, path, FNM_PATHNAME) && (file->events & IN_DELETE))1156 if (! fnmatch (full_path, path, FNM_PATHNAME) && (file->events & IN_DELETE))
1168 handle_event (handled, full_path, IN_DELETE, path);1157 handle_event (handled, full_path, IN_DELETE, path);
@@ -1423,9 +1412,9 @@
1423watched_dir_new (const char *path,1412watched_dir_new (const char *path,
1424 const struct stat *statbuf)1413 const struct stat *statbuf)
1425{1414{
1426 char watched_path[PATH_MAX];1415 nih_local char *watched_path = NULL;
1427 size_t len;1416 WatchedDir *dir;
1428 WatchedDir *dir;1417 size_t len;
14291418
1430 nih_assert (path);1419 nih_assert (path);
1431 nih_assert (statbuf);1420 nih_assert (statbuf);
@@ -1435,7 +1424,10 @@
14351424
1436 watched_dir_init ();1425 watched_dir_init ();
14371426
1438 strcpy (watched_path, path);1427 watched_path = nih_strdup (NULL, path);
1428 if (! watched_path)
1429 return NULL;
1430
1439 len = strlen (watched_path);1431 len = strlen (watched_path);
14401432
1441 if (len > 1 && watched_path[len-1] == '/') {1433 if (len > 1 && watched_path[len-1] == '/') {
@@ -1683,7 +1675,8 @@
16831675
1684 do {1676 do {
1685 /* save parent for next time through the loop */1677 /* save parent for next time through the loop */
1686 strcpy (tmp, current);1678 memset (tmp, '\0', sizeof (tmp));
1679 strncpy (tmp, current, sizeof (tmp)-1);
1687 parent = dirname (tmp);1680 parent = dirname (tmp);
16881681
1689 /* Ensure dirname returned something sane */1682 /* Ensure dirname returned something sane */

Subscribers

People subscribed via source and target branches