Skip to content

Commit

Permalink
Use stringutil::form/vform instead of asprintf/vasprintf
Browse files Browse the repository at this point in the history
This gets rid of the warnings:
warning: ignoring return value of 'int asprintf(char**, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]

which point to the error case where vasprintf cannot allocate memory
and the result is undefined, so the program would likely crash.
stringutil::form return an empty string in such case.
  • Loading branch information
mvidner committed May 11, 2015
1 parent bfb6aa8 commit 4831037
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 56 deletions.
12 changes: 6 additions & 6 deletions liby2/src/genericfrontend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
#include <YCP.h>
#include <ycp/Parser.h>
#include <ycp/pathsearch.h>
#include <y2util/stringutil.h>
#include "exitcodes.h"
#include <debugger/Debugger.h>

Expand Down Expand Up @@ -852,17 +853,16 @@ print_help()
static void
print_error (const char* format, ...)
{
char* msg;

va_list ap;
va_start (ap, format);
vasprintf (&msg, format, ap);

string str = stringutil::vform(format, ap);
const char * msg = str.c_str();

va_end (ap);

fprintf (stderr, "%s\n", msg);
fputs (msg, stderr);
y2error ("%s", msg);

free (msg);
}


Expand Down
34 changes: 15 additions & 19 deletions liby2util-r/src/include/y2util/stringutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ enum Trim {
TRIM = (L_TRIM|R_TRIM)
};

inline std::string vform( const char * format, va_list ap ) {
char * buf = 0;
std::string val;

int numprinted = vasprintf( &buf, format, ap );
if ( numprinted >= 0 ) {
val = buf;
free( buf );
}
return val;
}

inline std::string form( const char * format, ... )
__attribute__ ((format (printf, 1, 2)));

Expand All @@ -59,35 +71,19 @@ inline std::string form( const char * format, ... )
* </PRE>
**/
inline std::string form( const char * format, ... ) {
char * buf = 0;
std::string val;

va_list ap;
va_start( ap, format );

#if 1
int numprinted = vasprintf( &buf, format, ap );
if ( numprinted >= 0 ) {
val = buf;
free( buf );
}
#else
// Don't know wheter we actually nedd two va_lists, one to
// evaluate the buffer size needed, and one to actually fill
// the buffer. Maybe there's a save way to reuse a va_lists.
va_list ap1;
va_start( ap1, format );
buf = new char[vsnprintf( NULL, 0, format, ap ) + 1];
vsprintf( buf, format, ap1 );
val = buf;
delete [] buf;
va_end( ap1 );
#endif
val = vform(format, ap);

va_end( ap );
return val;
}



/**
* Print number. Optional second argument sets the minimal string width (' ' padded).
* Negative values will cause the number to be left adjusted within the string. Default
Expand Down
13 changes: 2 additions & 11 deletions liby2util-r/src/y2changes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@

/* Defines */

#define _GNU_SOURCE 1 /* Needed for vasprintf below */

#define Y2CHANGES_DATE "%Y-%m-%d %H:%M:%S" /* The date format */

// 1 timestamp, 2 level, 3 hostname
Expand Down Expand Up @@ -139,10 +137,7 @@ string y2_changesfmt_prefix (logcategory_t category)
char date[50]; // that's big enough
strftime (date, sizeof (date), Y2CHANGES_DATE, brokentime);

char * result_c = NULL;
asprintf (&result_c, Y2CHANGES_FORMAT, date, log_messages[category], hostname );
string result = result_c;
free (result_c);
string result = stringutil::form(Y2CHANGES_FORMAT, date, log_messages[category], hostname );

return result;
}
Expand All @@ -156,11 +151,7 @@ void y2changes_function (logcategory_t category, const char *format, ...)
va_start(ap, format);

/* Prepare the log text */
char *logtext = NULL;
vasprintf(&logtext, format, ap); /* GNU extension needs the define above */
string common = logtext;
common += '\n';
free (logtext);
string common = stringutil::vform(format, ap) + '\n';

if(log_to_syslog) {
syslog (LOG_NOTICE, Y2CHANGES_SYSLOG, category, common.c_str ());
Expand Down
28 changes: 8 additions & 20 deletions liby2util-r/src/y2log.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@

/* Defines */

#define _GNU_SOURCE 1 /* Needed for vasprintf below */

#define Y2LOG_DATE "%Y-%m-%d %H:%M:%S" /* The date format */

// 1 component, 2 file, 3 func, 4 line, 5 logtext, 6 eol
Expand Down Expand Up @@ -177,8 +175,7 @@ string y2_logfmt_common(bool simple, const string& component, const char *file,
const int line, const char *function, const char *format, va_list ap)
{
/* Prepare the log text */
char *logtext = NULL;
vasprintf(&logtext, format, ap); /* GNU extension needs the define above */
string logtext = stringutil::vform(format, ap);

/* Prepare the component */
string comp = component;
Expand Down Expand Up @@ -211,18 +208,12 @@ string y2_logfmt_common(bool simple, const string& component, const char *file,
func = "(" + func + ")";

/* do we need EOL? */
bool eol = false;
size_t len = strlen(logtext);
if ((len==0) || ((len>0) && (logtext[len-1]!='\n')))
eol = true;

char * result_c;
asprintf(&result_c, simple? Y2LOG_SIMPLE: Y2LOG_COMMON,
comp.c_str (), file, func.c_str (), line, logtext, eol?"\n":"");
string result = result_c;
free (result_c);

free (logtext);
bool need_eol = logtext.empty () || (logtext.back () != '\n');

string result = stringutil::form(simple ? Y2LOG_SIMPLE : Y2LOG_COMMON,
comp.c_str (), file, func.c_str (), line,
logtext.c_str (), need_eol?"\n":"");

return result;
}

Expand Down Expand Up @@ -267,10 +258,7 @@ string y2_logfmt_prefix (loglevel_t level)
strcat (date, tmp2);
#endif

char * result_c = NULL;
asprintf (&result_c, Y2LOG_FORMAT, date, level, hostname, pid);
string result = result_c;
free (result_c);
string result = stringutil::form(Y2LOG_FORMAT, date, level, hostname, pid);

return result;
}
Expand Down

0 comments on commit 4831037

Please sign in to comment.