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 <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Patrick Steinhardt committed
Jun 7, 2024 at 08:38 UTC
e7b40195ae0082d04ea8c0d1769d90ea700b76f2
3 files changed
+31
-15
compat/basename.c
+14
-2
@@ -10,7 +10,13 @@ char *gitbasename (char *path)
10
skip_dos_drive_prefix(&path);
11
12
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 *) ".";
20
21
for (base = path; *path; path++) {
22
if (!is_dir_sep(*path))
@@ -34,7 +40,13 @@ char *gitdirname(char *path)
40
int dos_drive_prefix;
41
42
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 *) ".";
50
51
if ((dos_drive_prefix = skip_dos_drive_prefix(&p)) && !*p)
52
goto dot;
compat/mingw.c
+16
-12
@@ -2279,7 +2279,11 @@ struct passwd *getpwuid(int uid)
2279
p->pw_name = user_name;
2280
p->pw_gecos = get_extended_user_info(NameDisplay);
2281
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";
2287
p->pw_dir = NULL;
2288
2289
initialized = 1;
@@ -2800,16 +2804,16 @@ int is_path_owned_by_current_sid(const char *path, struct strbuf *report)
2804
strbuf_addf(report, "'%s' is on a file system that does "
2805
"not record ownership\n", path);
2806
} 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;
2810
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;
2813
else
2814
str1 = "(inconvertible)";
2811
- if (ConvertSidToStringSidA(sid, &str2))
2812
- to_local_free2 = str2;
2815
+ if (ConvertSidToStringSidA(sid, &to_local_free2))
2816
+ str2 = to_local_free2;
2817
else
2818
str2 = "(inconvertible)";
2819
@@ -2822,13 +2826,13 @@ int is_path_owned_by_current_sid(const char *path, struct strbuf *report)
2826
str4 = "(invalid)";
2827
} else {
2828
if (user_sid_to_user_name(current_user_sid,
2825
- &str3))
2826
- to_free3 = str3;
2829
+ &to_free3))
2830
+ str3 = to_free3;
2831
else
2832
str3 = "(inconvertible)";
2833
if (ConvertSidToStringSidA(current_user_sid,
2830
- &str4))
2831
- to_local_free4 = str4;
2834
+ &to_local_free4))
2835
+ str4 = to_local_free4;
2836
else
2837
str4 = "(inconvertible)";
2838
}
compat/winansi.c
+1
-1
@@ -139,7 +139,7 @@ static void write_console(unsigned char *str, size_t len)
139
/* convert utf-8 to utf-16 */
140
int wlen = xutftowcsn(wbuf, (char*) str, ARRAY_SIZE(wbuf), len);
141
if (wlen < 0) {
142
- wchar_t *err = L"[invalid]";
142
+ const wchar_t *err = L"[invalid]";
143
WriteConsoleW(console, err, wcslen(err), &dummy, NULL);
144
return;
145
}