Revert "Use a special path to place the .o files in."
[oota-llvm.git] / runtime / libprofile / CommonProfiling.c
index 210a5e5ab78ae67920bbd92914e906253ea3d195..acc17ce11e01c1096896b474f431c477e474789e 100644 (file)
@@ -46,7 +46,7 @@ int save_arguments(int argc, const char **argv) {
      * what to do with it.
      */
     const char *Arg = argv[1];
-    memmove(&argv[1], &argv[2], (argc-1)*sizeof(char*));
+    memmove((char**)&argv[1], &argv[2], (argc-1)*sizeof(char*));
     --argc;
 
     if (!strcmp(Arg, "-llvmprof-output")) {
@@ -54,7 +54,7 @@ int save_arguments(int argc, const char **argv) {
         puts("-llvmprof-output requires a filename argument!");
       else {
         OutputFilename = strdup(argv[1]);
-        memmove(&argv[1], &argv[2], (argc-1)*sizeof(char*));
+        memmove((char**)&argv[1], &argv[2], (argc-1)*sizeof(char*));
         --argc;
       }
     } else {
@@ -65,6 +65,16 @@ int save_arguments(int argc, const char **argv) {
   for (Length = 0, i = 0; i != (unsigned)argc; ++i)
     Length += strlen(argv[i])+1;
 
+  /* Defensively check for a zero length, even though this is unlikely
+   * to happen in practice.  This avoids calling malloc() below with a
+   * size of 0.
+   */
+  if (Length == 0) {
+    SavedArgs = 0;
+    SavedArgsLength = 0;
+    return argc;
+  }
+  
   SavedArgs = (char*)malloc(Length);
   for (Length = 0, i = 0; i != (unsigned)argc; ++i) {
     unsigned Len = strlen(argv[i]);
@@ -102,12 +112,19 @@ int getOutFile() {
     {
       int PTy = ArgumentInfo;
       int Zeros = 0;
-      write(OutFile, &PTy, sizeof(int));
-      write(OutFile, &SavedArgsLength, sizeof(unsigned));
-      write(OutFile, SavedArgs, SavedArgsLength);
+      if (write(OutFile, &PTy, sizeof(int)) < 0 ||
+          write(OutFile, &SavedArgsLength, sizeof(unsigned)) < 0 ||
+          write(OutFile, SavedArgs, SavedArgsLength) < 0 ) {
+        fprintf(stderr,"error: unable to write to output file.");
+        exit(0);
+      }
       /* Pad out to a multiple of four bytes */
-      if (SavedArgsLength & 3)
-        write(OutFile, &Zeros, 4-(SavedArgsLength&3));
+      if (SavedArgsLength & 3) {
+        if (write(OutFile, &Zeros, 4-(SavedArgsLength&3)) < 0) {
+          fprintf(stderr,"error: unable to write to output file.");
+          exit(0);
+        }
+      }
     }
   }
   return(OutFile);