Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Make bprintf abort on error, and define bvprintf accordingly.
authoragiersch <agiersch@48e7efb5-ca39-0410-a469-dd3cf9ba447f>
Mon, 25 Oct 2010 11:33:37 +0000 (11:33 +0000)
committeragiersch <agiersch@48e7efb5-ca39-0410-a469-dd3cf9ba447f>
Mon, 25 Oct 2010 11:33:37 +0000 (11:33 +0000)
When there is no error checking, use bprintf or bvprintf instead of
asprintf or vasprintf.

git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@8451 48e7efb5-ca39-0410-a469-dd3cf9ba447f

examples/amok/bandwidth/bandwidth.c
include/simgrid_config.h.in
src/gras/DataDesc/ddt_create.c
src/simix/smx_global.c
src/xbt/cunit.c
src/xbt/snprintf.c
src/xbt/xbt_log_layout_format.c
src/xbt/xbt_log_layout_simple.c

index 28de73d..5ef8a16 100644 (file)
@@ -92,10 +92,10 @@ int maestro(int argc, char *argv[])
   gras_msg_handleall(5);        /* friends, we're ready. Come and play */
 
   if (xbt_dynar_length(group) < 2) {
-    CRITICAL1("Not enough peers arrived. Expected 2 got %ld",
-              xbt_dynar_length(group));
+    char *msg = bprintf("Not enough peers arrived. Expected 2 got %ld",
+                        xbt_dynar_length(group));
     amok_pm_group_shutdown("bandwidth");
-    xbt_abort();
+    xbt_die(msg);
   }
   h1 = *(xbt_peer_t *) xbt_dynar_get_ptr(group, 0);
   h2 = *(xbt_peer_t *) xbt_dynar_get_ptr(group, 1);
index 1735095..9070c40 100644 (file)
@@ -56,7 +56,7 @@ XBT_PUBLIC(long) getline(char **lineptr, size_t * n, FILE * stream);
 #endif
 #include <stdio.h>
 #endif
-
+#include <stdarg.h>
 
 /* snprintf related functions */
 /** @addtogroup XBT_str
@@ -79,12 +79,18 @@ XBT_PUBLIC(int) asprintf(char **ptr, const char *fmt,   /*args */
  * See asprintf()
  */
 #if defined(SIMGRID_NEED_VASPRINTF)||defined(DOXYGEN)
-#include <stdarg.h>
 XBT_PUBLIC(int) vasprintf(char **ptr, const char *fmt, va_list ap);
 #endif
 /** @brief print to allocated string
  *
- * Works just like asprintf(), but returns a pointer to the newly created string
+ * Works just like vasprintf(), but returns a pointer to the newly
+ * created string, or aborts on error.
+ */
+XBT_PUBLIC(char *) bvprintf(const char *fmt, va_list ap);
+/** @brief print to allocated string
+ *
+ * Works just like asprintf(), but returns a pointer to the newly
+ * created string, or aborts on error.
  */
 XBT_PUBLIC(char *) bprintf(const char *fmt, ...) _XBT_GNUC_PRINTF(1, 2);
 /** @} */
index 4c8e3e2..1000add 100644 (file)
@@ -694,8 +694,7 @@ gras_datadesc_dynar(gras_datadesc_type_t elm_t, void_f_pvoid_t free_func)
   char *buffname;
   gras_datadesc_type_t res;
 
-  if (asprintf(&buffname, "s_xbt_dynar_of_%s", elm_t->name) == -1)
-    xbt_die("asprintf failed");
+  buffname = bprintf("s_xbt_dynar_of_%s", elm_t->name);
 
   res = gras_datadesc_struct(buffname);
 
@@ -725,8 +724,7 @@ gras_datadesc_dynar(gras_datadesc_type_t elm_t, void_f_pvoid_t free_func)
 
   /* build a ref to it */
   free(buffname);
-  if (asprintf(&buffname, "xbt_dynar_of_%s", elm_t->name) == -1)
-    xbt_die("asprintf failed");
+  buffname = bprintf("xbt_dynar_of_%s", elm_t->name);
   res = gras_datadesc_ref(buffname, res);
   free(buffname);
   return res;
@@ -760,8 +758,7 @@ gras_datadesc_matrix(gras_datadesc_type_t elm_t,
   char *buffname;
   gras_datadesc_type_t res;
 
-  if (asprintf(&buffname, "s_xbt_matrix_t(%s)", elm_t->name) == -1)
-    xbt_die("asprintf failed");
+  buffname = bprintf("s_xbt_matrix_t(%s)", elm_t->name);
   res = gras_datadesc_struct(buffname);
 
   gras_datadesc_struct_append(res, "lines",
@@ -786,8 +783,7 @@ gras_datadesc_matrix(gras_datadesc_type_t elm_t,
 
   /* build a ref to it */
   free(buffname);
-  if (asprintf(&buffname, "xbt_matrix_t(%s)", elm_t->name) == -1)
-    xbt_die("asprintf failed");
+  buffname = bprintf("xbt_matrix_t(%s)", elm_t->name);
   res = gras_datadesc_ref(buffname, res);
   free(buffname);
   return res;
index 9be6131..c4a7995 100644 (file)
@@ -104,12 +104,11 @@ void SIMIX_display_process_status(void)
   xbt_swag_foreach(process, simix_global->process_list) {
     char *who, *who2;
 
-    if (asprintf(&who, "%s on %s: %s",
-                 process->name,
-                 process->smx_host->name,
-                 (process->blocked) ? "[BLOCKED] "
-                 : ((process->suspended) ? "[SUSPENDED] " : "")) == -1)
-      xbt_die("asprintf failed");
+    who = bprintf("%s on %s: %s",
+                  process->name,
+                  process->smx_host->name,
+                  (process->blocked) ? "[BLOCKED] "
+                  : ((process->suspended) ? "[SUSPENDED] " : ""));
 
     if (process->mutex) {
       who2 =
index 97f37ea..8efe254 100644 (file)
@@ -10,7 +10,7 @@
 
 #include "portable.h"
 
-#include "xbt/sysdep.h"         /* vasprintf */
+#include "xbt/sysdep.h"         /* bvprintf */
 #include "xbt/cunit.h"
 #include "xbt/dynar.h"
 
@@ -160,14 +160,13 @@ xbt_test_suite_t xbt_test_suite_new(const char *name, const char *fmt, ...)
 {
   xbt_test_suite_t suite = xbt_new0(struct s_xbt_test_suite, 1);
   va_list ap;
-  int vres;
 
   if (!_xbt_test_suites)
     _xbt_test_suites =
         xbt_dynar_new(sizeof(xbt_test_suite_t), xbt_test_suite_free);
 
   va_start(ap, fmt);
-  vres = vasprintf(&suite->title, fmt, ap);
+  suite->title = bvprintf(fmt, ap);
   suite->units =
       xbt_dynar_new(sizeof(xbt_test_unit_t), &xbt_test_unit_free);
   va_end(ap);
@@ -188,7 +187,6 @@ xbt_test_suite_t xbt_test_suite_by_name(const char *name, const char *fmt,
 
   char *bufname;
   va_list ap;
-  int vres;
 
   if (_xbt_test_suites)
     xbt_dynar_foreach(_xbt_test_suites, it_suite, suite)
@@ -196,7 +194,7 @@ xbt_test_suite_t xbt_test_suite_by_name(const char *name, const char *fmt,
       return suite;
 
   va_start(ap, fmt);
-  vres = vasprintf(&bufname, fmt, ap);
+  bufname = bvprintf(fmt, ap);
   va_end(ap);
   suite = xbt_test_suite_new(name, bufname, NULL);
   free(bufname);
@@ -226,7 +224,6 @@ void xbt_test_suite_push(xbt_test_suite_t suite, const char *name,
 {
   xbt_test_unit_t unit;
   va_list ap;
-  int vres;
 
   xbt_assert(suite);
   xbt_assert(func);
@@ -234,7 +231,7 @@ void xbt_test_suite_push(xbt_test_suite_t suite, const char *name,
 
   unit = xbt_new0(struct s_xbt_test_unit, 1);
   va_start(ap, fmt);
-  vres = vasprintf(&unit->title, fmt, ap);
+  unit->title = bvprintf(fmt, ap);
   va_end(ap);
   unit->name = (char *) name;
   unit->func = func;
@@ -260,7 +257,6 @@ static int xbt_test_suite_run(xbt_test_suite_t suite)
   unsigned int it_unit, it_test, it_log;
 
   int first = 1;                /* for result pretty printing */
-  int vres;
 
   if (suite == NULL)
     return 0;
@@ -299,9 +295,7 @@ static int xbt_test_suite_run(xbt_test_suite_t suite)
       unit->test_expect = 0;
 
       /* display unit title */
-      vres =
-          asprintf(&cp,
-                   " Unit: %s ......................................"
+      cp = bprintf(" Unit: %s ......................................"
                    "......................................", unit->title);
       cp[70] = '\0';
       fprintf(stderr, "%s", cp);
@@ -722,14 +716,13 @@ void _xbt_test_add(const char *file, int line, const char *fmt, ...)
   xbt_test_unit_t unit = _xbt_test_current_unit;
   xbt_test_test_t test;
   va_list ap;
-  int vres;
 
   xbt_assert(unit);
   xbt_assert(fmt);
 
   test = xbt_new0(struct s_xbt_test_test, 1);
   va_start(ap, fmt);
-  vres = vasprintf(&test->title, fmt, ap);
+  test->title = bvprintf(fmt, ap);
   va_end(ap);
   test->failed = 0;
   test->expected_failure = 0;
@@ -748,7 +741,6 @@ void _xbt_test_fail(const char *file, int line, const char *fmt, ...)
   xbt_test_test_t test;
   xbt_test_log_t log;
   va_list ap;
-  int vres;
 
   xbt_assert(unit);
   xbt_assert(fmt);
@@ -759,7 +751,7 @@ void _xbt_test_fail(const char *file, int line, const char *fmt, ...)
 
   log = xbt_new(struct s_xbt_test_log, 1);
   va_start(ap, fmt);
-  vres = vasprintf(&log->text, fmt, ap);
+  log->text = bvprintf(fmt, ap);
   va_end(ap);
   log->file = file;
   log->line = line;
@@ -807,7 +799,6 @@ void _xbt_test_log(const char *file, int line, const char *fmt, ...)
   xbt_test_test_t test;
   xbt_test_log_t log;
   va_list ap;
-  int vres;
 
   xbt_assert(unit);
   xbt_assert(fmt);
@@ -818,7 +809,7 @@ void _xbt_test_log(const char *file, int line, const char *fmt, ...)
 
   log = xbt_new(struct s_xbt_test_log, 1);
   va_start(ap, fmt);
-  vres = vasprintf(&log->text, fmt, ap);
+  log->text = bvprintf(fmt, ap);
   va_end(ap);
   log->file = file;
   log->line = line;
index d946499..cba8d0b 100644 (file)
@@ -1168,16 +1168,26 @@ int portable_vsnprintf(char *str, size_t str_m, const char *fmt,
   /* FIXME: better place */
 #include "xbt/sysdep.h"
 
+char *bvprintf(const char *fmt, va_list ap)
+{
+  char *res;
+
+  if (vasprintf(&res, fmt, ap) < 0) {
+    /* Do not want to use xbt_die() here, as it uses the logging
+     * infrastucture and may fail to allocate memory too. */
+    fprintf(stderr, "bprintf: vasprintf failed. Aborting.\n");
+    abort();
+  }
+  return res;
+}
+
 char *bprintf(const char *fmt, ...)
 {
   va_list ap;
   char *res;
-  int len;
 
   va_start(ap, fmt);
-
-  len = vasprintf(&res, fmt, ap);
-
+  res = bvprintf(fmt, ap);
   va_end(ap);
   return res;
 }
index 22a7cca..5eba2b8 100644 (file)
@@ -56,7 +56,6 @@ static void xbt_log_layout_format_dynamic(xbt_log_layout_t l,
   char *q = l->data;
   char *tmp;
   char *tmp2;
-  int vres;                     /* shut gcc up, but ignored */
 
   while (*q != '\0') {
     if (*q == '%') {
@@ -150,7 +149,7 @@ static void xbt_log_layout_format_dynamic(xbt_log_layout_t l,
         break;
 
       case 'm':                /* user-provided message; LOG4J compliant */
-        vres = vasprintf(&tmp2, fmt, ev->ap_copy);
+        tmp2 = bvprintf(fmt, ev->ap_copy);
         append1("%s", "%.*s", tmp2);
         free(tmp2);
         break;
index ddb77e9..e56adc8 100644 (file)
@@ -46,8 +46,7 @@ static void xbt_log_layout_simple_dynamic(xbt_log_layout_t l,
 
   xbt_strbuff_append(buff, loc_buff);
 
-  if (vasprintf(&p, fmt, ev->ap_copy) == -1)
-    xbt_die("vasprintf failed");
+  p = bvprintf(fmt, ev->ap_copy);
   xbt_strbuff_append(buff, p);
   free(p);