Skip to content

Commit

Permalink
portability: fix problems compiling with C++ compiler
Browse files Browse the repository at this point in the history
Michael McConville suggested removing a "redundant" cast of
the return value from malloc(), based on a change in the
OpenBSD version of unifdef. This is a good suggestion for
a purely C program, but the cast is required in C++.

Carsten Hey contributed some portability improvements in
2012 which included this cast, so that unifdef can be
compiled with a C++ compiler. Unfortunately since then
there have been several regressions in C++ support.

This commit fixes those regressions.
  • Loading branch information
fanf2 committed Feb 26, 2016
1 parent 5a40ec5 commit 87dfd91
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion unifdef.c
Original file line number Diff line number Diff line change
Expand Up @@ -1626,7 +1626,7 @@ xstrdup(const char *start, const char *end)

if (end < start) abort(); /* bug */
n = (size_t)(end - start) + 1;
s = malloc(n);
s = (char *)malloc(n);
if (s == NULL)
err(2, "malloc");
snprintf(s, n, "%s", start);
Expand Down
2 changes: 1 addition & 1 deletion win32/unifdef.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
#define snprintf c99_snprintf

/* win32.c */
int replace(const char *old, const char *new);
int replace(const char *oldname, const char *newname);
FILE *mktempmode(char *tmp, int mode);
FILE *fbinmode(FILE *fp);
int c99_snprintf(char *buf, size_t buflen, const char *format, ...);
Expand Down
10 changes: 5 additions & 5 deletions win32/win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@
* remove anything that might be in the way before renaming.
*/
int
replace(const char *old, const char *new)
replace(const char *oldname, const char *newname)
{
if (remove(new) < 0 && errno != ENOENT)
warn("can't remove \"%s\"", new);
return (rename(old, new));
if (remove(newname) < 0 && errno != ENOENT)
warn("can't remove \"%s\"", newname);
return (rename(oldname, newname));
}

FILE *
Expand Down Expand Up @@ -77,7 +77,7 @@ int c99_snprintf(char *buf, size_t buflen, const char *format, ...)
/* Paranoia about off-by-one errors in _snprintf() */
tmplen = outlen + 2;

tmp = malloc(tmplen);
tmp = (char *)malloc(tmplen);
if (tmp == NULL)
err(2, "malloc");
va_start(ap, format);
Expand Down

0 comments on commit 87dfd91

Please sign in to comment.