You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucy.apache.org by ma...@apache.org on 2006/10/16 02:03:24 UTC

svn commit: r464325 - in /lucene/lucy/trunk/charmonizer/src: Charmonizer.charm Charmonizer/Core.charm

Author: marvin
Date: Sun Oct 15 17:03:22 2006
New Revision: 464325

URL: http://svn.apache.org/viewvc?view=rev&rev=464325
Log:
Dynamically allocate the sprintf buffer for a system() command, allowing
compiler and ccflags to have arbitrary lengths.

Modified:
    lucene/lucy/trunk/charmonizer/src/Charmonizer.charm
    lucene/lucy/trunk/charmonizer/src/Charmonizer/Core.charm

Modified: lucene/lucy/trunk/charmonizer/src/Charmonizer.charm
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/charmonizer/src/Charmonizer.charm?view=diff&rev=464325&r1=464324&r2=464325
==============================================================================
--- lucene/lucy/trunk/charmonizer/src/Charmonizer.charm (original)
+++ lucene/lucy/trunk/charmonizer/src/Charmonizer.charm Sun Oct 15 17:03:22 2006
@@ -10,14 +10,7 @@
 chaz_set_compiler(char* comp_cmd) 
 {
     size_t len = strlen(comp_cmd);
-
-    /* security check */
-    if (len > 100)
-        die("compiler command greater than 100 characters: %s", comp_cmd);
-
-    /* assign new val */
-    free(compiler);
-    compiler = (char*)malloc(len * (sizeof(char)) + 1);
+    compiler = (char*)realloc(compiler, len * (sizeof(char)) + 1);
     compiler[len] = '\0';
     strncpy(compiler, comp_cmd, len);
 }
@@ -26,14 +19,7 @@
 chaz_set_ccflags(char* flag_string) 
 {
     size_t len = strlen(flag_string);
-
-    /* security check */
-    if (len > 100)
-        die("ccflags greater than 100 characters: %s", flag_string);
-
-    /* assign new val */
-    free(ccflags);
-    ccflags = (char*)malloc(len * (sizeof(char)) + 1);
+    ccflags = (char*)realloc(ccflags, len * (sizeof(char)) + 1);
     ccflags[len] = '\0';
     strncpy(ccflags, flag_string, len);
 }

Modified: lucene/lucy/trunk/charmonizer/src/Charmonizer/Core.charm
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/charmonizer/src/Charmonizer/Core.charm?view=diff&rev=464325&r1=464324&r2=464325
==============================================================================
--- lucene/lucy/trunk/charmonizer/src/Charmonizer/Core.charm (original)
+++ lucene/lucy/trunk/charmonizer/src/Charmonizer/Core.charm Sun Oct 15 17:03:22 2006
@@ -13,12 +13,14 @@
 char *typedef_prefix  = NULL;
 char *function_prefix = NULL;
 
+static char *command_buf = NULL;
+static size_t command_buf_len = 0;
+
 char*
 capture_output(char *source, size_t source_len, size_t *output_len) 
 {
     char *captured_output = NULL;
     FILE *app_source_fh, *targ_fh, *garbage_fh;
-    char  command[256];
     int   command_len = 0;
     int   successful;
     
@@ -36,19 +38,27 @@
     if (fclose(app_source_fh))
         die("Error closing file '%s': %s", TRY_SOURCE_PATH, strerror(errno));
 
-    
-    /* security note: snprintf isn't available, so we just have to make sure
-     * that the compiler command is short enough at an earlier stage.
-     */
-    sprintf(command, "%s " TRY_SOURCE_PATH " -o " TRY_APP_PATH " %s\0", 
-        compiler, ccflags);
+    /* allocate space for the sprintf'd command we'll pass to system() */
+    if (compiler == NULL)
+        die("compiler has not been set");
+    if (ccflags == NULL)
+        die("ccflags has not been set");
+    command_len = strlen(compiler) + strlen(ccflags) + 200;
+    if (command_len > command_buf_len) {
+        command_buf = realloc(command_buf, command_len);
+        if (command_buf == NULL) 
+            die("allocation of command_buf failed");
+        command_buf_len = command_len;
+    }
 
     /* Compile the source.
      * Unfortunately there's no completely portable way to redirect stderr
      * yet be able to recover it later, so we're stuck seeing the
      * errors when something fails.
      */
-    system(command);
+    sprintf(command_buf, "%s " TRY_SOURCE_PATH " -o " TRY_APP_PATH " %s\0", 
+        compiler, ccflags);
+    system(command_buf);
 
     /* see if compilation was successful */
     garbage_fh = fopen(TRY_APP_PATH, "r");
@@ -58,11 +68,8 @@
 
     /* if compilation was successful... */
     if (successful) {
-        /* run the app */
-        sprintf(command, "./" TRY_APP_PATH "\0");
-        system(command);
-
-        /* slurp the app's output and write it to our fh */
+        /* run the app, slurp the output and write it to our fh */
+        system("./" TRY_APP_PATH);
         captured_output = slurp_file(TARGET_PATH, output_len);
     }
     else {