"GOT", but the "O" is a cute, smiling pufferfish. Index | Thread | Search

From:
Omar Polo <op@omarpolo.com>
Subject:
*printf return value
To:
gameoftrees@openbsd.org
Date:
Wed, 10 Aug 2022 14:37:43 +0200

Download raw body.

Thread
today re-reading some other code was reminded that the *printf family
of functions returns "a value less than 0" on output or encoding
errors (with asprintf being a notable exception that always returns -1
on error.)

In got I've found only one place where we were missing a check on the
return of fprintf: in lib/diff.c the return of fprintf is used to
compute `outoff' for further usage later in the function.  If it's
negative nothing bad happens, we will probably fail later when trying
to use a negative offset.

I've also spotted some unneeded checks for snprintf.  The correct
check would be `ret < 0', but since snprintf can only fail due to an
encoding error, I think we can just check for the partial write.

very nitpicking, sorry :/

ok?

diff /home/op/w/got
commit - bf80b15220f51490025e916633cdd70816113604
path + /home/op/w/got
blob - f671a4f105400e52465bc32e860ae0b0b1e71afd
file + lib/diff.c
--- lib/diff.c
+++ lib/diff.c
@@ -903,6 +903,9 @@ show_object_id(struct got_diff_line **lines, size_t *n
 	off_t outoff = 0;
 
 	n = fprintf(outfile, "%s %c %s\n", obj_typestr, ch, id_str);
+	if (n < 0)
+		return got_error_from_errno("fprintf");
+
 	if (lines != NULL && *lines != NULL) {
 		if (*nlines == 0) {
 			err = add_line_metadata(lines, nlines, 0,
blob - 9ce30e5bcc35c7934d3795a978a487d7624ef00b
file + lib/error.c
--- lib/error.c
+++ lib/error.c
@@ -364,7 +364,7 @@ got_error_no_obj(struct got_object_id *id)
 		return got_error(GOT_ERR_NO_OBJ);
 
 	ret = snprintf(msg, sizeof(msg), "object %s not found", id_str);
-	if (ret == -1 || ret >= sizeof(msg))
+	if (ret >= sizeof(msg))
 		return got_error(GOT_ERR_NO_OBJ);
 
 	return got_error_msg(GOT_ERR_NO_OBJ, msg);
@@ -377,7 +377,7 @@ got_error_not_ref(const char *refname)
 	int ret;
 
 	ret = snprintf(msg, sizeof(msg), "reference %s not found", refname);
-	if (ret == -1 || ret >= sizeof(msg))
+	if (ret >= sizeof(msg))
 		return got_error(GOT_ERR_NOT_REF);
 
 	return got_error_msg(GOT_ERR_NOT_REF, msg);
blob - 05652e3057827e3b2c8cc76f5e95d07a081979a7
file + lib/object_create.c
--- lib/object_create.c
+++ lib/object_create.c
@@ -272,7 +272,7 @@ te_mode2str(char *buf, size_t len, struct got_tree_ent
 		return got_error(GOT_ERR_BAD_FILETYPE);
 
 	ret = snprintf(buf, len, "%o ", mode);
-	if (ret == -1 || ret >= len)
+	if (ret >= len)
 		return got_error(GOT_ERR_NO_SPACE);
 	return NULL;
 }