Unix Technical Forum

Re: more improvements for mg

This is a discussion on Re: more improvements for mg within the mailing.openbsd.tech forums, part of the OpenBSD category; --> Todd C. Miller wrote: > In message <20050516151748.GA1337@moule.localdomain> > so spake Alexandre Ratchov (alex-contact): > > > int ret; ...


Go Back   Unix Technical Forum > Unix Operating Systems > OpenBSD > mailing.openbsd.tech

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-18-2008, 08:50 AM
Han Boetes
 
Posts: n/a
Default Re: more improvements for mg

Todd C. Miller wrote:
> In message <20050516151748.GA1337@moule.localdomain>
> so spake Alexandre Ratchov (alex-contact):
>
> > int ret;
> > char foo[100];
> >
> > ...
> > ret = snprintf(foo, sizeof(foo), "%s", input);
> > if (ret < 0 || (size_t)ret > = sizeof(foo))
> >
> >
> > -1 means that snprintf failed and negative number of chars is meaningless.
> > This works if sizeof(foo) > INT_MAX because res is converted to size_t.
> > Have i missed something?

>
> While this does look OK I really don't see the need for the cast
> to size_t. In practice sizeof(foo) will not be > INT_MAX unless
> you are creating 2GB data structures on the stack and the stack
> is not big enough to hold such a data structure anyway.


Thanks for clarifying.

That leaves us with the following patch:

Index: def.h
================================================== =================
RCS file: /cvs/src/usr.bin/mg/def.h,v
retrieving revision 1.60
diff -u -p -w -r1.60 def.h
--- def.h 3 Apr 2005 02:09:28 -0000 1.60
+++ def.h 16 May 2005 16:27:06 -0000
@@ -134,8 +134,8 @@ typedef struct {
typedef struct LINE {
struct LINE *l_fp; /* Link to the next line */
struct LINE *l_bp; /* Link to the previous line */
- int l_size; /* Allocated size */
- int l_used; /* Used size */
+ size_t l_size; /* Allocated size */
+ size_t l_used; /* Used size */
char *l_text; /* Content of the line */
} LINE;

Index: fileio.c
================================================== =================
RCS file: /cvs/src/usr.bin/mg/fileio.c,v
retrieving revision 1.49
diff -u -p -w -r1.49 fileio.c
--- fileio.c 3 Apr 2005 02:09:28 -0000 1.49
+++ fileio.c 16 May 2005 16:27:06 -0000
@@ -298,17 +298,18 @@ startupfile(char *suffix)
{
static char file[NFILEN];
char *home;
+ int ret;

if ((home = getenv("HOME")) == NULL || *home == '\0')
goto nohome;

if (suffix == NULL) {
- if (snprintf(file, sizeof(file), "%s/.mg", home)
- >= sizeof(file))
+ ret = snprintf(file, sizeof(file), "%s/.mg", home);
+ if (ret < 0 || ret >= sizeof(file))
return (NULL);
} else {
- if (snprintf(file, sizeof(file), "%s/.mg-%s", home, suffix)
- >= sizeof(file))
+ ret = snprintf(file, sizeof(file), "%s/.mg-%s", home, suffix);
+ if (ret < 0 || ret >= sizeof(file))
return (NULL);
}

@@ -317,12 +318,12 @@ startupfile(char *suffix)
nohome:
#ifdef STARTUPFILE
if (suffix == NULL) {
- if (snprintf(file, sizeof(file), "%s", STARTUPFILE)
- >= sizeof(file))
+ ret = snprintf(file, sizeof(file), "%s", STARTUPFILE);
+ if (ret < 0 || ret >= sizeof(file))
return (NULL);
} else {
- if (snprintf(file, sizeof(file), "%s%s", STARTUPFILE, suffix)
- >= sizeof(file))
+ ret = snprintf(file, sizeof(file), "%s%s", STARTUPFILE, suffix);
+ if (ret < 0 || ret >= sizeof(file))
return (NULL);
}

@@ -395,6 +396,7 @@ dired_(char *dirname)
FILE *dirpipe;
char line[256];
int len;
+ int ret;

if ((dirname = adjustname(dirname)) == NULL) {
ewprintf("Bad directory name");
@@ -413,8 +415,8 @@ dired_(char *dirname)
if (bclear(bp) != TRUE)
return (NULL);
bp->b_flag |= BFREADONLY;
- if (snprintf(line, sizeof(line), "ls -al %s", dirname)
- >= sizeof(line)) {
+ ret = snprintf(line, sizeof(line), "ls -al %s", dirname);
+ if (ret < 0 || ret >= sizeof(line)) {
ewprintf("Path too long");
return (NULL);
}
@@ -487,6 +489,7 @@ make_file_list(char *buf)
LIST *last;
struct filelist *current;
char prefixx[NFILEN + 1];
+ int ret;

/*
* We need three different strings: dir - the name of the directory
@@ -576,10 +579,10 @@ make_file_list(char *buf)
char statname[NFILEN + 2];

statbuf.st_mode = 0;
- if (snprintf(statname, sizeof(statname), "%s/%s",
- dir, dent->d_name) > sizeof(statname) - 1) {
+ ret = snprintf(statname, sizeof(statname), "%s/%s",
+ dir, dent->d_name);
+ if (ret < 0 || ret >= sizeof(statname))
continue;
- }
if (stat(statname, &statbuf) < 0)
continue;
if (statbuf.st_mode & S_IFDIR)
@@ -590,9 +593,9 @@ make_file_list(char *buf)
if (current == NULL)
break;

- if (snprintf(current->fl_name, sizeof(current->fl_name),
- "%s%s%s", prefixx, dent->d_name, isdir ? "/" : "")
- >= sizeof(current->fl_name)) {
+ ret = snprintf(current->fl_name, sizeof(current->fl_name),
+ "%s%s%s", prefixx, dent->d_name, isdir ? "/" : "");
+ if (ret < 0 || ret >= sizeof(current->fl_name)) {
free(current);
continue;
}



# Han

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:05 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com