Skip to content

Commit 59b5d98

Browse files
peffgitster
authored andcommitted
pkt-line: make packet_reader.line non-const
The "line" member of a packet_reader struct is marked as const. This kind of makes sense, because it's not its own allocated buffer that should be freed, and we often use const to indicate that. But it is always writable, because it points into the non-const "buffer" member. And we rely on this writability in places like send-pack and receive-pack, where we parse incoming packet contents by writing NULs over delimiters. This has traditionally worked because we implicitly cast away the constness with strchr() like: const char *head; char *p; head = reader->line; p = strchr(head, ' '); Since C23 libc provides a generic strchr() to detect this implicit const removal, this now generate a compiler warning on some platforms (like recent glibc). We can fix it by marking "line" as non-const, as well as a few intermediate variables (like "head" in the above example). Note that by itself, switching to a non-const variable would cause problems with this line in send-pack.c: if (!skip_prefix(reader->line, "unpack ", &reader->line)) But due to our skip_prefix() magic introduced in the previous commit, this compiles fine (both the in and out-parameters are non-const, so we know it is safe). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 2b64df0 commit 59b5d98

3 files changed

Lines changed: 9 additions & 7 deletions

File tree

builtin/receive-pack.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,8 +1025,8 @@ static int read_proc_receive_report(struct packet_reader *reader,
10251025

10261026
for (;;) {
10271027
struct object_id old_oid, new_oid;
1028-
const char *head;
1029-
const char *refname;
1028+
char *head;
1029+
char *refname;
10301030
char *p;
10311031
enum packet_read_status status;
10321032

@@ -1050,7 +1050,8 @@ static int read_proc_receive_report(struct packet_reader *reader,
10501050
}
10511051
*p++ = '\0';
10521052
if (!strcmp(head, "option")) {
1053-
const char *key, *val;
1053+
char *key;
1054+
const char *val;
10541055

10551056
if (!hint || !(report || new_report)) {
10561057
if (!once++)

pkt-line.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ struct packet_reader {
184184
int pktlen;
185185

186186
/* the last line read */
187-
const char *line;
187+
char *line;
188188

189189
/* indicates if a line has been peeked */
190190
int line_peeked;

send-pack.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,8 @@ static int receive_status(struct repository *r,
175175
ret = receive_unpack_status(reader);
176176
while (1) {
177177
struct object_id old_oid, new_oid;
178-
const char *head;
179-
const char *refname;
178+
char *head;
179+
char *refname;
180180
char *p;
181181
if (packet_reader_read(reader) != PACKET_READ_NORMAL)
182182
break;
@@ -190,7 +190,8 @@ static int receive_status(struct repository *r,
190190
*p++ = '\0';
191191

192192
if (!strcmp(head, "option")) {
193-
const char *key, *val;
193+
char *key;
194+
const char *val;
194195

195196
if (!hint || !(report || new_report)) {
196197
if (!once++)

0 commit comments

Comments
 (0)