Skip to content

Commit e7b4019

Browse files
pks-tgitster
authored andcommitted
compat/win32: fix const-correctness with string constants
Adjust various places in our Win32 compatibility layer where we are not assigning string constants to `const char *` variables. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9c076c3 commit e7b4019

File tree

3 files changed

+31
-15
lines changed

3 files changed

+31
-15
lines changed

compat/basename.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@ char *gitbasename (char *path)
1010
skip_dos_drive_prefix(&path);
1111

1212
if (!path || !*path)
13-
return ".";
13+
/*
14+
* basename(3P) is mis-specified because it returns a
15+
* non-constant pointer even though it is specified to return a
16+
* pointer to internal memory at times. The cast is a result of
17+
* that.
18+
*/
19+
return (char *) ".";
1420

1521
for (base = path; *path; path++) {
1622
if (!is_dir_sep(*path))
@@ -34,7 +40,13 @@ char *gitdirname(char *path)
3440
int dos_drive_prefix;
3541

3642
if (!p)
37-
return ".";
43+
/*
44+
* dirname(3P) is mis-specified because it returns a
45+
* non-constant pointer even though it is specified to return a
46+
* pointer to internal memory at times. The cast is a result of
47+
* that.
48+
*/
49+
return (char *) ".";
3850

3951
if ((dos_drive_prefix = skip_dos_drive_prefix(&p)) && !*p)
4052
goto dot;

compat/mingw.c

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2279,7 +2279,11 @@ struct passwd *getpwuid(int uid)
22792279
p->pw_name = user_name;
22802280
p->pw_gecos = get_extended_user_info(NameDisplay);
22812281
if (!p->pw_gecos)
2282-
p->pw_gecos = "unknown";
2282+
/*
2283+
* Data returned by getpwuid(3P) is treated as internal and
2284+
* must never be written to or freed.
2285+
*/
2286+
p->pw_gecos = (char *) "unknown";
22832287
p->pw_dir = NULL;
22842288

22852289
initialized = 1;
@@ -2800,16 +2804,16 @@ int is_path_owned_by_current_sid(const char *path, struct strbuf *report)
28002804
strbuf_addf(report, "'%s' is on a file system that does "
28012805
"not record ownership\n", path);
28022806
} else if (report) {
2803-
LPSTR str1, str2, str3, str4, to_free1 = NULL,
2804-
to_free3 = NULL, to_local_free2 = NULL,
2805-
to_local_free4 = NULL;
2807+
PCSTR str1, str2, str3, str4;
2808+
LPSTR to_free1 = NULL, to_free3 = NULL,
2809+
to_local_free2 = NULL, to_local_free4 = NULL;
28062810

2807-
if (user_sid_to_user_name(sid, &str1))
2808-
to_free1 = str1;
2811+
if (user_sid_to_user_name(sid, &to_free1))
2812+
str1 = to_free1;
28092813
else
28102814
str1 = "(inconvertible)";
2811-
if (ConvertSidToStringSidA(sid, &str2))
2812-
to_local_free2 = str2;
2815+
if (ConvertSidToStringSidA(sid, &to_local_free2))
2816+
str2 = to_local_free2;
28132817
else
28142818
str2 = "(inconvertible)";
28152819

@@ -2822,13 +2826,13 @@ int is_path_owned_by_current_sid(const char *path, struct strbuf *report)
28222826
str4 = "(invalid)";
28232827
} else {
28242828
if (user_sid_to_user_name(current_user_sid,
2825-
&str3))
2826-
to_free3 = str3;
2829+
&to_free3))
2830+
str3 = to_free3;
28272831
else
28282832
str3 = "(inconvertible)";
28292833
if (ConvertSidToStringSidA(current_user_sid,
2830-
&str4))
2831-
to_local_free4 = str4;
2834+
&to_local_free4))
2835+
str4 = to_local_free4;
28322836
else
28332837
str4 = "(inconvertible)";
28342838
}

compat/winansi.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ static void write_console(unsigned char *str, size_t len)
139139
/* convert utf-8 to utf-16 */
140140
int wlen = xutftowcsn(wbuf, (char*) str, ARRAY_SIZE(wbuf), len);
141141
if (wlen < 0) {
142-
wchar_t *err = L"[invalid]";
142+
const wchar_t *err = L"[invalid]";
143143
WriteConsoleW(console, err, wcslen(err), &dummy, NULL);
144144
return;
145145
}

0 commit comments

Comments
 (0)