checkpolicy (3)
PATCH 1/1 checkpolicy (3)
- fix calls to fwrite (size_t instead of int)
file: write.c
Joerg
write.c renamed from ../upstream/current/checkpolicy/write.c +58 -58
@@ -18,17 +18,17 @@ count++; buf[2 ] =cpu_to_le32 (count); - items =fwrite (buf,sizeof (__u32),3 , fp); + items =fwrite (buf,sizeof (__u32), (size_t )3 , fp);if (items !=3 )return -1 ;for (n = e->node; n; n = n->next) { bit =cpu_to_le32 (n->startbit); - items =fwrite (&bit,sizeof (__u32),1 , fp); + items =fwrite (&bit,sizeof (__u32), (size_t )1 , fp);if (items !=1 )return -1 ; map =cpu_to_le64 (n->map); - items =fwrite (&map,sizeof (__u64),1 , fp); + items =fwrite (&map,sizeof (__u64), (size_t )1 , fp);if (items !=1 )return -1 ;
@@ -46,7 +46,7 @@size_t items, items2; nel =cpu_to_le32 (a->nel); - items =fwrite (&nel,sizeof (__u32),1 , fp); + items =fwrite (&nel,sizeof (__u32), (size_t )1 , fp);if (items !=1 )return -1 ;
@@ -101,10 +101,10 @@ FILE * fp) {__u32 sens; -int items; +size_t items; sens =cpu_to_le32 (l->sens); - items =fwrite (&sens,sizeof (__u32),1 , fp); + items =fwrite (&sens,sizeof (__u32), (size_t )1 , fp);if (items !=1 )return -1 ;
@@ -123,7 +123,7 @@ FILE * fp) {__u32 buf[3 ]; -int items, items2; +size_t items, items2;int rel; rel =mls_level_relation (r->level[1 ], r->level[0 ]);
@@ -163,7 +163,7 @@ {mls_perms_t *p = &cladatum->mlsperms;__u32 buf[32 ]; -int items, items2; +size_t items, items2; items =0 ; buf[items++] =cpu_to_le32 (p->read);
@@ -191,7 +191,7 @@for (r = usrdatum->ranges; r; r = r->next) nel++; buf[0 ] =cpu_to_le32 (nel); - items =fwrite (buf,sizeof (__u32),1 , fp); + items =fwrite (buf,sizeof (__u32), (size_t )1 , fp);if (items !=1 )return -1 ;for (r = usrdatum->ranges; r; r = r->next) {
@@ -207,7 +207,7 @@size_t items; buf[0 ] =cpu_to_le32 (p->nlevels); - items =fwrite (buf,sizeof (__u32),1 , fp); + items =fwrite (buf,sizeof (__u32), (size_t )1 , fp);if (items !=1 )return -1 ;return 0 ;
@@ -227,8 +227,8 @@int sens_write (hashtab_key_t key,hashtab_datum_t datum,void *p) {level_datum_t *levdatum; -__u32 buf[32 ], len; -int items, items2; +__u32 buf[32 ]; +size_t items, items2, len;FILE *fp = p; levdatum = (level_datum_t *) datum;
@@ -241,7 +241,7 @@if (items != items2)return -1 ; - items =fwrite (key,1 , len, fp); + items =fwrite (key, (size_t )1 , len, fp);if (items != len)return -1 ;
@@ -254,8 +254,8 @@int cat_write (hashtab_key_t key,hashtab_datum_t datum,void *p) {cat_datum_t *catdatum; -__u32 buf[32 ], len; -int items, items2; +__u32 buf[32 ]; +size_t items, items2, len;FILE *fp = p;
@@ -317,8 +317,8 @@static int perm_write (hashtab_key_t key,hashtab_datum_t datum,void *p) {perm_datum_t *perdatum; -__u32 buf[32 ], len; -int items, items2; +__u32 buf[32 ]; +size_t items, items2, len;FILE *fp = p; perdatum = (perm_datum_t *) datum;
@@ -332,7 +332,7 @@if (items != items2)return -1 ; - items =fwrite (key,1 , len, fp); + items =fwrite (key, (size_t )1 , len, fp);if (items != len)return -1 ;
@@ -343,8 +343,8 @@static int common_write (hashtab_key_t key,hashtab_datum_t datum,void *p) {common_datum_t *comdatum; -__u32 buf[32 ], len; -int items, items2; +__u32 buf[32 ]; +size_t items, items2, len;FILE *fp = p; comdatum = (common_datum_t *) datum;
@@ -359,7 +359,7 @@if (items != items2)return -1 ; - items =fwrite (key,1 , len, fp); + items =fwrite (key, (size_t )1 , len, fp);if (items != len)return -1 ;
@@ -375,8 +375,8 @@class_datum_t *cladatum;constraint_node_t *c;constraint_expr_t *e; -__u32 buf[32 ], len, len2, ncons, nexpr; -int items, items2; +__u32 buf[32 ], ncons, nexpr; +size_t items, items2, len, len2;FILE *fp = p; cladatum = (class_datum_t *) datum;
@@ -406,12 +406,12 @@if (items != items2)return -1 ; - items =fwrite (key,1 , len, fp); + items =fwrite (key, (size_t )1 , len, fp);if (items != len)return -1 ;if (cladatum->comkey) { - items =fwrite (cladatum->comkey,1 , len2, fp); + items =fwrite (cladatum->comkey, (size_t )1 , len2, fp);if (items != len2)return -1 ; }
@@ -425,7 +425,7 @@ } buf[0 ] =cpu_to_le32 (c->permissions); buf[1 ] =cpu_to_le32 (nexpr); - items =fwrite (buf,sizeof (__u32),2 , fp); + items =fwrite (buf,sizeof (__u32), (size_t )2 , fp);if (items !=2 )return -1 ;for (e = c->expr; e; e = e->next) {
@@ -457,8 +457,8 @@static int role_write (hashtab_key_t key,hashtab_datum_t datum,void *p) {role_datum_t *role; -__u32 buf[32 ], len; -int items, items2; +__u32 buf[32 ]; +size_t items, items2, len;FILE *fp = p; role = (role_datum_t *) datum;
@@ -471,7 +471,7 @@if (items != items2)return -1 ; - items =fwrite (key,1 , len, fp); + items =fwrite (key, (size_t )1 , len, fp);if (items != len)return -1 ;
@@ -487,8 +487,8 @@static int type_write (hashtab_key_t key,hashtab_datum_t datum,void *p) {type_datum_t *typdatum; -__u32 buf[32 ], len; -int items, items2; +__u32 buf[32 ]; +size_t items, items2, len;FILE *fp = p; typdatum = (type_datum_t *) datum;
@@ -502,7 +502,7 @@if (items != items2)return -1 ; - items =fwrite (key,1 , len, fp); + items =fwrite (key, (size_t )1 , len, fp);if (items != len)return -1 ;
@@ -512,8 +512,8 @@static int user_write (hashtab_key_t key,hashtab_datum_t datum,void *p) {user_datum_t *usrdatum; -__u32 buf[32 ], len; -int items, items2; +__u32 buf[32 ]; +size_t items, items2, len;FILE *fp = p;
@@ -527,7 +527,7 @@if (items != items2)return -1 ; - items =fwrite (key,1 , len, fp); + items =fwrite (key, (size_t )1 , len, fp);if (items != len)return -1 ;
@@ -561,8 +561,8 @@ocontext_t *c;genfs_t *genfs;int i, j; -__u32 buf[32 ], len, config, nel; -size_t items, items2; +__u32 buf[32 ], config; +size_t items, items2, len, nel;char *policydb_str =POLICYDB_STRING ; config =0 ;
@@ -576,7 +576,7 @@ items2 =fwrite (buf,sizeof (__u32), items, fp);if (items != items2)return -1 ; - items =fwrite (policydb_str,1 , len, fp); + items =fwrite (policydb_str, (size_t )1 , len, fp);if (items != len)return -1 ;
@@ -596,7 +596,7 @@for (i =0 ; i <SYM_NUM ; i++) { buf[0 ] =cpu_to_le32 (p->symtab[i].nprim); buf[1 ] =cpu_to_le32 (p->symtab[i].table->nel); - items =fwrite (buf,sizeof (__u32),2 , fp); + items =fwrite (buf,sizeof (__u32), (size_t )2 , fp);if (items !=2 )return -1 ;if (hashtab_map (p->symtab[i].table, write_f[i], fp))
@@ -610,14 +610,14 @@for (tr = p->role_tr; tr; tr = tr->next) nel++; buf[0 ] =cpu_to_le32 (nel); - items =fwrite (buf,sizeof (__u32),1 , fp); + items =fwrite (buf,sizeof (__u32), (size_t )1 , fp);if (items !=1 )return -1 ;for (tr = p->role_tr; tr; tr = tr->next) { buf[0 ] =cpu_to_le32 (tr->role); buf[1 ] =cpu_to_le32 (tr->type); buf[2 ] =cpu_to_le32 (tr->new_role); - items =fwrite (buf,sizeof (__u32),3 , fp); + items =fwrite (buf,sizeof (__u32), (size_t )3 , fp);if (items !=3 )return -1 ; }
@@ -626,13 +626,13 @@for (ra = p->role_allow; ra; ra = ra->next) nel++; buf[0 ] =cpu_to_le32 (nel); - items =fwrite (buf,sizeof (__u32),1 , fp); + items =fwrite (buf,sizeof (__u32), (size_t )1 , fp);if (items !=1 )return -1 ;for (ra = p->role_allow; ra; ra = ra->next) { buf[0 ] =cpu_to_le32 (ra->role); buf[1 ] =cpu_to_le32 (ra->new_role); - items =fwrite (buf,sizeof (__u32),2 , fp); + items =fwrite (buf,sizeof (__u32), (size_t )2 , fp);if (items !=2 )return -1 ; }
@@ -642,14 +642,14 @@for (c = p->ocontexts[i]; c; c = c->next) nel++; buf[0 ] =cpu_to_le32 (nel); - items =fwrite (buf,sizeof (__u32),1 , fp); + items =fwrite (buf,sizeof (__u32), (size_t )1 , fp);if (items !=1 )return -1 ;for (c = p->ocontexts[i]; c; c = c->next) {switch (i) {case OCON_ISID : buf[0 ] =cpu_to_le32 (c->sid[0 ]); - items =fwrite (buf,sizeof (__u32),1 , fp); + items =fwrite (buf,sizeof (__u32), (size_t )1 , fp);if (items !=1 )return -1 ;if (context_write (&c->context[0 ], fp))
@@ -659,10 +659,10 @@case OCON_NETIF : len =strlen (c->u.name); buf[0 ] =cpu_to_le32 (len); - items =fwrite (buf,sizeof (__u32),1 , fp); + items =fwrite (buf,sizeof (__u32), (size_t )1 , fp);if (items !=1 )return -1 ; - items =fwrite (c->u.name,1 , len, fp); + items =fwrite (c->u.name, (size_t )1 , len, fp);if (items != len)return -1 ;if (context_write (&c->context[0 ], fp))
@@ -677,7 +677,7 @@for (j =0 ; j <3 ; j++) { buf[j] =cpu_to_le32 (buf[j]); } - items =fwrite (buf,sizeof (__u32),3 , fp); + items =fwrite (buf,sizeof (__u32), (size_t )3 , fp);if (items !=3 )return -1 ;if (context_write (&c->context[0 ], fp))
@@ -686,7 +686,7 @@case OCON_NODE : buf[0 ] =cpu_to_le32 (c->u.node.addr); buf[1 ] =cpu_to_le32 (c->u.node.mask); - items =fwrite (buf,sizeof (__u32),2 , fp); + items =fwrite (buf,sizeof (__u32), (size_t )2 , fp);if (items !=2 )return -1 ;if (context_write (&c->context[0 ], fp))
@@ -696,10 +696,10 @@ buf[0 ] =cpu_to_le32 (c->v.behavior); len =strlen (c->u.name); buf[1 ] =cpu_to_le32 (len); - items =fwrite (buf,sizeof (__u32),2 , fp); + items =fwrite (buf,sizeof (__u32), (size_t )2 , fp);if (items !=2 )return -1 ; - items =fwrite (c->u.name,1 , len, fp); + items =fwrite (c->u.name, (size_t )1 , len, fp);if (items != len)return -1 ;if (context_write (&c->context[0 ], fp))
@@ -713,36 +713,36 @@for (genfs = p->genfs; genfs; genfs = genfs->next) nel++; buf[0 ] =cpu_to_le32 (nel); - items =fwrite (buf,sizeof (__u32),1 , fp); + items =fwrite (buf,sizeof (__u32), (size_t )1 , fp);if (items !=1 )return -1 ;for (genfs = p->genfs; genfs; genfs = genfs->next) { len =strlen (genfs->fstype); buf[0 ] =cpu_to_le32 (len); - items =fwrite (buf,sizeof (__u32),1 , fp); + items =fwrite (buf,sizeof (__u32), (size_t )1 , fp);if (items !=1 )return -1 ; - items =fwrite (genfs->fstype,1 , len, fp); + items =fwrite (genfs->fstype, (size_t )1 , len, fp);if (items != len)return -1 ; nel =0 ;for (c = genfs->head; c; c = c->next) nel++; buf[0 ] =cpu_to_le32 (nel); - items =fwrite (buf,sizeof (__u32),1 , fp); + items =fwrite (buf,sizeof (__u32), (size_t )1 , fp);if (items !=1 )return -1 ;for (c = genfs->head; c; c = c->next) { len =strlen (c->u.name); buf[0 ] =cpu_to_le32 (len); - items =fwrite (buf,sizeof (__u32),1 , fp); + items =fwrite (buf,sizeof (__u32), (size_t )1 , fp);if (items !=1 )return -1 ; - items =fwrite (c->u.name,1 , len, fp); + items =fwrite (c->u.name, (size_t )1 , len, fp);if (items != len)return -1 ; buf[0 ] =cpu_to_le32 (c->v.sclass); - items =fwrite (buf,sizeof (__u32),1 , fp); + items =fwrite (buf,sizeof (__u32), (size_t )1 , fp);if (items !=1 )return -1 ;if (context_write (&c->context[0 ], fp))
Quoted 2 lines · Joerg Hoh
- fix calls to fwrite (
size_tinstead of int)
file:write.c
Why? In particular, what possible point is there in casting constant size values (e.g. 3) to size_t?
Signature · 8 lines
Stephen Smalley <[email protected]> National Security Agency -- This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.
Quoted 6 lines · Stephen Smalley
Quoted 2 lines · Joerg Hoh
- fix calls to fwrite (
size_tinstead of int)
file:write.cWhy? In particular, what possible point is there in casting constant size values (e.g. 3) to
size_t?
Constant values are ints. man 3 fwrite states on my system:
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
int is always 32bit, even when size_t is 64bit.
Joerg
--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.
Quoted 5 lines · Joerg Hoh
Constant values are ints. man 3 fwrite states on my system:
size_tfwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);int is always 32bit, even when
size_tis 64bit.
Type conversion should be automatic in this case, no explicit cast required. At most, you might want to qualify the constant in some manner, e.g. 3UL, but I'm not sure whether even that is justified.
Signature · 8 lines
Stephen Smalley <[email protected]> National Security Agency -- This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.
Quoted 10 lines · Stephen Smalley
Quoted 5 lines · Joerg Hoh
Constant values are ints. man 3 fwrite states on my system:
size_tfwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);int is always 32bit, even when
size_tis 64bit.Type conversion should be automatic in this case, no explicit cast required. At most, you might want to qualify the constant in some manner, e.g. 3UL, but I'm not sure whether even that is justified.
The gcc on my alpha produces warnings, so there is no implicit conversion. We should make it explicit.
I prefer the cast via "(size_t)" over "3z", because most people probably don't know, what that "z" means (it's the qualifier for size_t and ssize_t).
Joerg
--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.
Quoted 2 lines · Joerg Hoh
The gcc on my alpha produces warnings, so there is no implicit conversion. We should make it explicit.
What is the warning? And what warning flags are you using?
Warnings are not errors.
Signature · 8 lines
Stephen Smalley <[email protected]> National Security Agency -- This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.
Quoted 7 lines · Stephen Smalley
Quoted 2 lines · Joerg Hoh
The gcc on my alpha produces warnings, so there is no implicit conversion. We should make it explicit.
What is the warning? And what warning flags are you using?
Warnings are not errors.
For better checking we temporarily enabled -Wconversion:
write.c: In function `class_write':
write.c:405: warning: passing arg 3 of fwrite' with different width due to prototype write.c:409: warning: passing arg 2 of fwrite' with different width due to prototype write.c:409: warning: passing arg 3 of fwrite' with different width due to prototype write.c:414: warning: passing arg 2 of fwrite' with different width due to prototype write.c:414: warning: passing arg 3 of fwrite' with different width due to prototype write.c:428: warning: passing arg 3 of fwrite' with different width due to prototype write.c:436: warning: passing arg 3 of `fwrite' with different width due to prototype (and many more warnings)
These switch isn't enabled with -Wall.
(from the manpage:)
-Wconversion
Warn if a prototype causes a type conversion that is different from what would happen to the same argument in the absence of a prototype. This includes conversions of fixed point to floating and vice versa, and conver- sions changing the width or signedness of a fixed point argument except when the same as the default promo- tion.
Also, warn if a negative integer constant expression is implicitly converted to an unsigned type. For exam- ple, warn about the assignment "x = -1" if "x" is unsigned. But do not warn about explicit casts like "(unsigned) -1".
The compiler itself does the this automatic cast (and only warns about that); yeah, we may exclude this patch.
Joerg
--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.
Quoted 2 lines · Joerg Hoh
The compiler itself does the this automatic cast (and only warns about that); yeah, we may exclude this patch.
Ok, so let's consider the changes in this patch besides the casting of constants, i.e. changing certain variables from int to size_t since they are used to store the return value of functions that return size_t, e.g. fwrite, strlen. Using size_t for these variables avoids truncation on the return value, but note that some of these variables are subsequently assigned to u32 entries in a buf array (after conversion to little endian) and written out to the binary policy file. So truncation of the value is still possible, and I'm puzzled that the compiler is not warning about this if sizeof(size_t) != sizeof(u32).
Signature · 8 lines
Stephen Smalley <[email protected]> National Security Agency -- This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.