init.c asprintf

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

init.c asprintf

David Hill-3
More asprintf cleanup for init.c.  This also checks return failures.

- David
Index: src/sbin/init/init.c
===================================================================
RCS file: /cvs/src/sbin/init/init.c,v
retrieving revision 1.36
diff -u -r1.36 init.c
--- src/sbin/init/init.c 13 Mar 2005 13:53:23 -0000 1.36
+++ src/sbin/init/init.c 12 Nov 2005 01:30:42 -0000
@@ -857,7 +857,6 @@
 new_session(session_t *sprev, int session_index, struct ttyent *typ)
 {
  session_t *sp;
- size_t len;
 
  if ((typ->ty_status & TTY_ON) == 0 ||
     typ->ty_name == 0 ||
@@ -870,9 +869,8 @@
  sp->se_flags = SE_PRESENT;
  sp->se_index = session_index;
 
- len = sizeof(_PATH_DEV) + strlen(typ->ty_name);
- sp->se_device = malloc(len);
- (void) snprintf(sp->se_device, len, "%s%s", _PATH_DEV, typ->ty_name);
+ if (asprintf(&sp->se_device, "%s%s", _PATH_DEV, typ->ty_name) == -1)
+ err(1, "asprintf");
 
  if (setupargv(sp, typ) == 0) {
  free_session(sp);
@@ -897,15 +895,12 @@
 int
 setupargv(session_t *sp, struct ttyent *typ)
 {
- size_t len;
-
  if (sp->se_getty) {
  free(sp->se_getty);
  free(sp->se_getty_argv);
  }
- len = strlen(typ->ty_getty) + strlen(typ->ty_name) + 2;
- sp->se_getty = malloc(len);
- (void) snprintf(sp->se_getty, len, "%s %s", typ->ty_getty, typ->ty_name);
+ if (asprintf(&sp->se_getty, "%s %s", typ->ty_getty, typ->ty_name) == -1)
+ err(1, "asprintf");
  sp->se_getty_argv = construct_argv(sp->se_getty);
  if (sp->se_getty_argv == 0) {
  warning("can't parse getty for port %s", sp->se_device);