{"id":2128,"date":"2022-08-30T15:22:17","date_gmt":"2022-08-30T15:22:17","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/01\/02\/why-do-i-get-a-seg-fault-on-ubuntu-but-not-mac-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:22:17","modified_gmt":"2022-08-30T15:22:17","slug":"why-do-i-get-a-seg-fault-on-ubuntu-but-not-mac-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/why-do-i-get-a-seg-fault-on-ubuntu-but-not-mac-collection-of-common-programming-errors\/","title":{"rendered":"Why do I get a seg fault on Ubuntu but not mac?-Collection of common programming errors"},"content":{"rendered":"<p>I have a program that checks the modification time of a file and executes the file if it has changed. Currently it works if I run it on my mac, but it seg faults if I run it on ubuntu. Please help me.<\/p>\n<p>note: this is in c<\/p>\n<pre><code>#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\n#define CONTERROR(cond, statement) \\\n   if (cond) { \\\n      perror(statement); \\\n      continue; \\\n   }\n#define FATALERROR(cond, statement) \\\n   if (cond) { \\\n      perror(statement); \\\n      exit(EXIT_FAILURE); \\\n   }\n\n\/**\n * Handler for the signals.\n *\/\nstatic void handler(int signum) {\n   ;\n}\n\n\/**\n * Main.\n *\/\nint main(int argc, char *argv[]) {\n   struct sigaction sa;\n   struct stat buf;\n   struct itimerval tb;\n   pid_t pid;\n   int modTime;\n\n   if (argc != 2) {\n      fprintf(stderr, \"usage: remote file\\n\");\n      exit(EXIT_FAILURE);\n   }\n\n   FATALERROR(stat(argv[1], &amp;buf) == -1, \"stat\");\n   modTime = buf.st_mtime;\n\n   tb.it_interval.tv_sec = 0;\n   tb.it_interval.tv_usec = 50000;\n   tb.it_value.tv_sec = 0;\n   tb.it_value.tv_usec = 50000;\n\n   setitimer(ITIMER_REAL, &amp;tb, 0);\n\n   sa.sa_handler = handler;\n   FATALERROR(sigemptyset(&amp;sa.sa_mask) == -1, \"mask\");\n   FATALERROR(sigaction(SIGALRM, &amp;sa, NULL) == -1, \"sigaction\");\n\n   while (1) {\n      pause();\n      CONTERROR(stat(argv[1], &amp;buf) == -1, \"stat\");\n      if (modTime != buf.st_mtime) {\n         modTime = buf.st_mtime;\n         pid = fork();\n         FATALERROR(pid == -1, \"fork\");\n         if (!pid) {\n            execlp(\"rexec\", \"rexec\", NULL);\n            fprintf(stderr, \"exec\\n\");\n         }\n      }\n   }\n   exit(EXIT_SUCCESS);\n}\n<\/code><\/pre>\n<ol>\n<li>\n<p>Most of your sigaction structure is not initialized, so could contain random data. If sa_flags.SA_SIGINFO is accidentally set in this uninitialized data, then the signal will cause sa_sigaction instead of sa_handler to be called, which is also uninitialized, so will almost certainly crash.<\/p>\n<p>You may find it easier to debug if you initialize all the fields, including making sure you have set the flags in a way the ensures the signals behaves the way you want.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2014-01-02 12:07:35. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I have a program that checks the modification time of a file and executes the file if it has changed. Currently it works if I run it on my mac, but it seg faults if I run it on ubuntu. Please help me. note: this is in c #include #include #include #include #include #include #include [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2128","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2128","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2128"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2128\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2128"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2128"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}