tedious string copying changes
authorCampbell Barton <ideasman42@gmail.com>
Fri, 5 Nov 2010 07:35:21 +0000 (07:35 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Fri, 5 Nov 2010 07:35:21 +0000 (07:35 +0000)
- use sizeof() in more places.
- fixed some off by 1 bugs copying strings. setting curve font family for instance was 1 char too short.
- replace strncpy and strcpy with BLI_strncpy

24 files changed:
source/blender/blenkernel/intern/action.c
source/blender/blenkernel/intern/anim_sys.c
source/blender/blenkernel/intern/armature.c
source/blender/blenkernel/intern/blender.c
source/blender/blenkernel/intern/constraint.c
source/blender/blenkernel/intern/curve.c
source/blender/blenkernel/intern/font.c
source/blender/blenkernel/intern/ipo.c
source/blender/blenkernel/intern/library.c
source/blender/blenloader/intern/writefile.c
source/blender/editors/armature/editarmature.c
source/blender/editors/armature/editarmature_retarget.c
source/blender/editors/object/object_edit.c
source/blender/editors/object/object_hook.c
source/blender/editors/space_file/filelist.c
source/blender/editors/space_image/image_ops.c
source/blender/editors/space_node/drawnode.c
source/blender/editors/space_outliner/outliner.c
source/blender/editors/space_view3d/view3d_buttons.c
source/blender/makesrna/intern/rna_controller.c
source/blender/makesrna/intern/rna_curve.c
source/blender/windowmanager/intern/wm.c
source/blender/windowmanager/intern/wm_files.c
source/blender/windowmanager/intern/wm_window.c

index 9028071..227f2ea 100644 (file)
@@ -408,7 +408,7 @@ bPoseChannel *verify_pose_channel(bPose *pose, const char *name)
        /* If not, create it and add it */
        chan = MEM_callocN(sizeof(bPoseChannel), "verifyPoseChannel");
        
-       strncpy(chan->name, name, 31);
+       BLI_strncpy(chan->name, name, sizeof(chan->name));
        /* init vars to prevent math errors */
        chan->quat[0] = chan->rotAxis[1]= 1.0f;
        chan->size[0] = chan->size[1] = chan->size[2] = 1.0f;
@@ -774,7 +774,7 @@ void pose_add_group (Object *ob)
                return;
        
        grp= MEM_callocN(sizeof(bActionGroup), "PoseGroup");
-       strcpy(grp->name, "Group");
+       BLI_strncpy(grp->name, "Group", sizeof(grp->name));
        BLI_addtail(&pose->agroups, grp);
        BLI_uniquename(&pose->agroups, grp, "Group", '.', offsetof(bActionGroup, name), sizeof(grp->name));
        
@@ -1119,8 +1119,8 @@ void what_does_obaction (Scene *UNUSED(scene), Object *ob, Object *workob, bPose
        
        workob->pose= pose;     /* need to set pose too, since this is used for both types of Action Constraint */
 
-       strcpy(workob->parsubstr, ob->parsubstr);
-       strcpy(workob->id.name, "OB<ConstrWorkOb>"); /* we don't use real object name, otherwise RNA screws with the real thing */
+       BLI_strncpy(workob->parsubstr, ob->parsubstr, sizeof(workob->parsubstr));
+       BLI_strncpy(workob->id.name, "OB<ConstrWorkOb>", sizeof(workob->id.name)); /* we don't use real object name, otherwise RNA screws with the real thing */
        
        /* if we're given a group to use, it's likely to be more efficient (though a bit more dangerous) */
        if (agrp) {
index e03799f..febe800 100644 (file)
@@ -614,12 +614,9 @@ KeyingSet *BKE_keyingset_add (ListBase *list, const char name[], short flag, sho
        
        /* allocate new KeyingSet */
        ks= MEM_callocN(sizeof(KeyingSet), "KeyingSet");
-       
-       if (name)
-               strncpy(ks->name, name, sizeof(ks->name));
-       else
-               strcpy(ks->name, "KeyingSet");
-       
+
+       BLI_strncpy(ks->name, name ? name : "KeyingSet", sizeof(ks->name));
+
        ks->flag= flag;
        ks->keyingflag= keyingflag;
        
@@ -667,7 +664,7 @@ KS_Path *BKE_keyingset_add_path (KeyingSet *ks, ID *id, const char group_name[],
        if (group_name)
                BLI_snprintf(ksp->group, 64, group_name);
        else
-               strcpy(ksp->group, "");
+               ksp->group[0]= '\0';
        
        /* store additional info for relative paths (just in case user makes the set relative) */
        if (id)
index 3bec79e..ea01421 100644 (file)
@@ -260,7 +260,7 @@ int bone_autoside_name (char *name, int UNUSED(strip_number), short axis, float
 
        len= strlen(name);
        if (len == 0) return 0;
-       strcpy(basename, name);
+       BLI_strncpy(basename, name, sizeof(basename));
        
        /* Figure out extension to append: 
         *      - The extension to append is based upon the axis that we are working on.
index 72d194e..9b2c180 100644 (file)
@@ -460,13 +460,13 @@ static UndoElem *curundo= NULL;
 
 static int read_undosave(bContext *C, UndoElem *uel)
 {
-       char mainstr[FILE_MAXDIR+FILE_MAXFILE];
+       char mainstr[sizeof(G.main->name)];
        int success=0, fileflags;
        
        /* This is needed so undoing/redoing doesnt crash with threaded previews going */
        WM_jobs_stop_all(CTX_wm_manager(C));
 
-       strcpy(mainstr, G.main->name);  /* temporal store */
+       BLI_strncpy(mainstr, G.main->name, sizeof(mainstr));    /* temporal store */
 
        fileflags= G.fileflags;
        G.fileflags |= G_FILE_NO_UI;
index bf69473..a561df4 100644 (file)
@@ -674,7 +674,7 @@ static void default_get_tarmat (bConstraint *con, bConstraintOb *cob, bConstrain
                ct= MEM_callocN(sizeof(bConstraintTarget), "tempConstraintTarget"); \
                 \
                ct->tar= datatar; \
-               strcpy(ct->subtarget, datasubtarget); \
+               BLI_strncpy(ct->subtarget, datasubtarget, sizeof(ct->subtarget)); \
                ct->space= con->tarspace; \
                ct->flag= CONSTRAINT_TAR_TEMP; \
                 \
index a7dd80b..b6b6dde 100644 (file)
@@ -155,7 +155,7 @@ Curve *add_curve(char *name, int type)
                cu->vfont= cu->vfontb= cu->vfonti= cu->vfontbi= get_builtin_font();
                cu->vfont->id.us+=4;
                cu->str= MEM_mallocN(12, "str");
-               strcpy(cu->str, "Text");
+               BLI_strncpy(cu->str, "Text", 12);
                cu->len= cu->pos= 4;
                cu->strinfo= MEM_callocN(12*sizeof(CharInfo), "strinfo new");
                cu->totbox= cu->actbox= 1;
index 0b27aae..5fdb954 100644 (file)
@@ -394,7 +394,7 @@ VFont *load_vfont(char *name)
 
                        /* if there's a font name, use it for the ID name */
                        if (strcmp(vfd->name, "")!=0) {
-                               BLI_strncpy(vfont->id.name+2, vfd->name, 21);
+                               BLI_strncpy(vfont->id.name+2, vfd->name, sizeof(vfont->id.name)-2);
                        }
                        BLI_strncpy(vfont->name, name, sizeof(vfont->name));
 
index 5ed6bea..7914bc2 100644 (file)
@@ -1017,13 +1017,13 @@ static ChannelDriver *idriver_to_cdriver (IpoDriver *idriver)
                                dtar= &dvar->targets[0];
                                dtar->id= (ID *)idriver->ob;
                                if (idriver->name[0])
-                                       BLI_strncpy(dtar->pchan_name, idriver->name, 32);
+                                       BLI_strncpy(dtar->pchan_name, idriver->name, sizeof(dtar->pchan_name));
                                
                                        /* second bone target (name was stored in same var as the first one) */
                                dtar= &dvar->targets[1];
                                dtar->id= (ID *)idriver->ob;
                                if (idriver->name[0]) // xxx... for safety
-                                       BLI_strncpy(dtar->pchan_name, idriver->name+DRIVER_NAME_OFFS, 32);
+                                       BLI_strncpy(dtar->pchan_name, idriver->name+DRIVER_NAME_OFFS, sizeof(dtar->pchan_name));
                        }
                        else {
                                /* only a single variable, of type 'transform channel' */
@@ -1034,7 +1034,7 @@ static ChannelDriver *idriver_to_cdriver (IpoDriver *idriver)
                                dtar= &dvar->targets[0];
                                dtar->id= (ID *)idriver->ob;
                                if (idriver->name[0])
-                                       BLI_strncpy(dtar->pchan_name, idriver->name, 32);
+                                       BLI_strncpy(dtar->pchan_name, idriver->name, sizeof(dtar->pchan_name));
                                dtar->transChan= adrcode_to_dtar_transchan(idriver->adrcode);
                                dtar->flag |= DTAR_FLAG_LOCALSPACE; /* old drivers took local space */
                        }
index 4227c63..ed9907b 100644 (file)
@@ -1365,7 +1365,7 @@ void rename_id(ID *id, char *name)
 {
        ListBase *lb;
 
-       strncpy(id->name+2, name, 21);
+       BLI_strncpy(id->name+2, name, sizeof(id->name)-2);
        lb= which_libbase(G.main, GS(id->name) );
        
        new_id(lb, id, name);                           
index b571535..14c74df 100644 (file)
@@ -506,7 +506,7 @@ static void write_renderinfo(WriteData *wd, Main *mainvar)          /* for renderdeamon
                        data[1]= sce->r.efra;
 
                        memset(data+2, 0, sizeof(int)*6);
-                       strncpy((char *)(data+2), sce->id.name+2, 21);
+                       BLI_strncpy((char *)(data+2), sce->id.name+2, sizeof(sce->id.name)-2);
 
                        writedata(wd, REND, 32, data);
                }
index cab2fdc..dcfc276 100644 (file)
@@ -170,7 +170,7 @@ EditBone *make_boneList(ListBase *edbo, ListBase *bones, EditBone *parent, Bone
                
                /*      Copy relevant data from bone to eBone */
                eBone->parent= parent;
-               BLI_strncpy(eBone->name, curBone->name, 32);
+               BLI_strncpy(eBone->name, curBone->name, sizeof(eBone->name));
                eBone->flag = curBone->flag;
                
                /* fix selection flags */
@@ -332,9 +332,9 @@ void ED_armature_from_edit(Object *obedit)
                newBone= MEM_callocN(sizeof(Bone), "bone");
                eBone->temp= newBone;   /* Associate the real Bones with the EditBones */
                
-               BLI_strncpy(newBone->name, eBone->name, 32);
-               memcpy(newBone->head, eBone->head, sizeof(float)*3);
-               memcpy(newBone->tail, eBone->tail, sizeof(float)*3);
+               BLI_strncpy(newBone->name, eBone->name, sizeof(newBone->name));
+               memcpy(newBone->head, eBone->head, sizeof(newBone->head));
+               memcpy(newBone->tail, eBone->tail, sizeof(newBone->tail));
                newBone->flag= eBone->flag;
                
                if (eBone == arm->act_edbone) {
@@ -758,7 +758,7 @@ static void joined_armature_fix_links(Object *tarArm, Object *srcArm, bPoseChann
                                                        
                                                        for (achan= act->chanbase.first; achan; achan= achan->next) {
                                                                if (strcmp(achan->name, pchan->name)==0)
-                                                                       BLI_strncpy(achan->name, curbone->name, 32);
+                                                                       BLI_strncpy(achan->name, curbone->name, sizeof(achan->name));
                                                        }
                                                }
                                        }
@@ -802,7 +802,7 @@ static void joined_armature_fix_links(Object *tarArm, Object *srcArm, bPoseChann
                        if (ob->partype==PARBONE) {
                                /* bone name in object */
                                if (!strcmp(ob->parsubstr, pchan->name))
-                                       BLI_strncpy(ob->parsubstr, curbone->name, 32);
+                                       BLI_strncpy(ob->parsubstr, curbone->name, sizeof(ob->parsubstr));
                        }
                        
                        /* make tar armature be new parent */
@@ -2331,7 +2331,7 @@ EditBone *ED_armature_edit_bone_add(bArmature *arm, char *name)
 {
        EditBone *bone= MEM_callocN(sizeof(EditBone), "eBone");
        
-       BLI_strncpy(bone->name, name, 32);
+       BLI_strncpy(bone->name, name, sizeof(bone->name));
        unique_editbone_name(arm->edbo, bone->name, NULL);
        
        BLI_addtail(arm->edbo, bone);
@@ -2663,7 +2663,7 @@ EditBone *duplicateEditBoneObjects(EditBone *curBone, char *name, ListBase *edit
        
        if (name != NULL)
        {
-               BLI_strncpy(eBone->name, name, 32);
+               BLI_strncpy(eBone->name, name, sizeof(eBone->name));
        }
 
        unique_editbone_name(editbones, eBone->name, NULL);
@@ -3507,7 +3507,7 @@ static int armature_extrude_exec(bContext *C, wmOperator *op)
                                        newbone->segments= 1;
                                        newbone->layer= ebone->layer;
                                        
-                                       BLI_strncpy (newbone->name, ebone->name, 32);
+                                       BLI_strncpy (newbone->name, ebone->name, sizeof(newbone->name));
                                        
                                        if (flipbone && forked) {       // only set if mirror edit
                                                if (strlen(newbone->name)<30) {
index e80f149..749443b 100644 (file)
@@ -1637,7 +1637,7 @@ static EditBone *add_editbonetolist(char *name, ListBase *list)
 {
        EditBone *bone= MEM_callocN(sizeof(EditBone), "eBone");
        
-       BLI_strncpy(bone->name, name, 32);
+       BLI_strncpy(bone->name, name, sizeof(bone->name));
        unique_editbone_name(list, bone->name, NULL);
        
        BLI_addtail(list, bone);
index 32e49ce..118c649 100644 (file)
@@ -1323,7 +1323,7 @@ void copy_attr(Main *bmain, Scene *scene, View3D *v3d, short event)
                                                BKE_text_to_curve(scene, base->object, 0);              /* needed? */
 
                                                
-                                               strcpy(cu1->family, cu->family);
+                                               BLI_strncpy(cu1->family, cu->family, sizeof(cu1->family));
                                                
                                                base->object->recalc |= OB_RECALC_DATA;
                                        }
index 3c3e7f1..ee71640 100644 (file)
@@ -447,7 +447,7 @@ static void add_hook_object(Main *bmain, Scene *scene, Object *obedit, Object *o
        hmd->indexar= indexar;
        copy_v3_v3(hmd->cent, cent);
        hmd->totindex= tot;
-       BLI_strncpy(hmd->name, name, 32);
+       BLI_strncpy(hmd->name, name, sizeof(hmd->name));
        
        /* matrix calculus */
        /* vert x (obmat x hook->imat) x hook->obmat x ob->imat */
index 43d5d54..41ad7ad 100644 (file)
@@ -887,7 +887,7 @@ static int groupname_to_code(char *group)
        char buf[32];
        char *lslash;
        
-       BLI_strncpy(buf, group, 31);
+       BLI_strncpy(buf, group, sizeof(buf));
        lslash= BLI_last_slash(buf);
        if (lslash)
                lslash[0]= '\0';
index 19af0b3..50730e3 100644 (file)
@@ -798,7 +798,7 @@ static int replace_exec(bContext *C, wmOperator *op)
                return OPERATOR_CANCELLED;
        
        RNA_string_get(op->ptr, "filepath", str);
-       BLI_strncpy(sima->image->name, str, sizeof(sima->image->name)-1); /* we cant do much if the str is longer then 240 :/ */
+       BLI_strncpy(sima->image->name, str, sizeof(sima->image->name)); /* we cant do much if the str is longer then 240 :/ */
 
        BKE_image_signal(sima->image, &sima->iuser, IMA_SIGNAL_RELOAD);
        WM_event_add_notifier(C, NC_IMAGE|NA_EDITED, sima->image);
index aef1c7b..c34ade4 100644 (file)
@@ -198,7 +198,7 @@ static void node_browse_tex_cb(bContext *C, void *ntree_v, void *node_v)
 
        node->id= &tex->id;
        id_us_plus(node->id);
-       BLI_strncpy(node->name, node->id->name+2, 21);
+       BLI_strncpy(node->name, node->id->name+2, sizeof(node->name));
        
        nodeSetActive(ntree, node);
        
@@ -285,7 +285,7 @@ static void node_browse_text_cb(bContext *C, void *ntree_v, void *node_v)
        oldid= node->id;
        node->id= BLI_findlink(&bmain->text, node->menunr-1);
        id_us_plus(node->id);
-       BLI_strncpy(node->name, node->id->name+2, 21); /* huh? why 21? */
+       BLI_strncpy(node->name, node->id->name+2, sizeof(node->name));
 
        node->custom1= BSET(node->custom1, NODE_DYNAMIC_NEW);
        
index f73da37..a9b0dc8 100644 (file)
@@ -5054,11 +5054,11 @@ static void namebutton_cb(bContext *C, void *tsep, char *oldname)
                                bArmature *arm= (bArmature *)tselem->id;
                                if(arm->edbo) {
                                        EditBone *ebone= te->directdata;
-                                       char newname[32];
+                                       char newname[sizeof(ebone->name)];
                                        
                                        /* restore bone name */
-                                       BLI_strncpy(newname, ebone->name, 32);
-                                       BLI_strncpy(ebone->name, oldname, 32);
+                                       BLI_strncpy(newname, ebone->name, sizeof(ebone->name));
+                                       BLI_strncpy(ebone->name, oldname, sizeof(ebone->name));
                                        ED_armature_bone_rename(obedit->data, oldname, newname);
                                        WM_event_add_notifier(C, NC_OBJECT|ND_POSE, OBACT);
                                }
@@ -5069,15 +5069,15 @@ static void namebutton_cb(bContext *C, void *tsep, char *oldname)
                                {
                                        Bone *bone= te->directdata;
                                        Object *ob;
-                                       char newname[32];
+                                       char newname[sizeof(bone->name)];
                                        
                                        // always make current object active
                                        tree_element_set_active_object(C, scene, soops, te, 1);
                                        ob= OBACT;
                                        
                                        /* restore bone name */
-                                       BLI_strncpy(newname, bone->name, 32);
-                                       BLI_strncpy(bone->name, oldname, 32);
+                                       BLI_strncpy(newname, bone->name, sizeof(bone->name));
+                                       BLI_strncpy(bone->name, oldname, sizeof(bone->name));
                                        ED_armature_bone_rename(ob->data, oldname, newname);
                                        WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
                                }
@@ -5086,15 +5086,15 @@ static void namebutton_cb(bContext *C, void *tsep, char *oldname)
                                {
                                        bPoseChannel *pchan= te->directdata;
                                        Object *ob;
-                                       char newname[32];
+                                       char newname[sizeof(pchan->name)];
                                        
                                        // always make current object active
                                        tree_element_set_active_object(C, scene, soops, te, 1);
                                        ob= OBACT;
                                        
                                        /* restore bone name */
-                                       BLI_strncpy(newname, pchan->name, 32);
-                                       BLI_strncpy(pchan->name, oldname, 32);
+                                       BLI_strncpy(newname, pchan->name, sizeof(pchan->name));
+                                       BLI_strncpy(pchan->name, oldname, sizeof(pchan->name));
                                        ED_armature_bone_rename(ob->data, oldname, newname);
                                        WM_event_add_notifier(C, NC_OBJECT|ND_POSE, ob);
                                }
index 450b372..9d17bbf 100644 (file)
@@ -884,14 +884,14 @@ static void v3d_posearmature_buts(uiLayout *layout, Object *ob)
 void validate_editbonebutton_cb(bContext *C, void *bonev, void *namev)
 {
        EditBone *eBone= bonev;
-       char oldname[32], newname[32];
-       
+       char oldname[sizeof(eBone->name)], newname[sizeof(eBone->name)];
+
        /* need to be on the stack */
-       BLI_strncpy(newname, eBone->name, 32);
-       BLI_strncpy(oldname, (char *)namev, 32);
+       BLI_strncpy(newname, eBone->name, sizeof(eBone->name));
+       BLI_strncpy(oldname, (char *)namev, sizeof(eBone->name));
        /* restore */
-       BLI_strncpy(eBone->name, oldname, 32);
-       
+       BLI_strncpy(eBone->name, oldname, sizeof(eBone->name));
+
        ED_armature_bone_rename(CTX_data_edit_object(C)->data, oldname, newname); // editarmature.c
        WM_event_add_notifier(C, NC_OBJECT|ND_BONE_SELECT, CTX_data_edit_object(C)); // XXX fix
 }
index ee0707a..73ac46e 100644 (file)
@@ -207,7 +207,6 @@ void RNA_def_controller(BlenderRNA *brna)
 
        prop= RNA_def_property(srna, "expression", PROP_STRING, PROP_NONE);
        RNA_def_property_string_sdna(prop, NULL, "str");
-       RNA_def_property_string_maxlength(prop, 127);
        RNA_def_property_ui_text(prop, "Expression", "");
        RNA_def_property_update(prop, NC_LOGIC, NULL);
 
index 7983b8f..c7802de 100644 (file)
@@ -844,7 +844,7 @@ static void rna_def_font(BlenderRNA *brna, StructRNA *srna)
        
        /* strings */
        prop= RNA_def_property(srna, "family", PROP_STRING, PROP_NONE);
-       RNA_def_property_string_maxlength(prop, 21);
+       RNA_def_property_string_maxlength(prop, (sizeof((ID *)NULL)->name)-2);
        RNA_def_property_ui_text(prop, "Object Font", "Use Blender Objects as font characters. Give font objects a common name followed by the character it represents, eg. familya, familyb etc, and turn on Verts Duplication");
        RNA_def_property_update(prop, 0, "rna_Curve_update_data");
        
index 4260692..76b5762 100644 (file)
@@ -275,7 +275,7 @@ void wm_add_default(bContext *C)
        win= wm_window_new(C);
        win->screen= screen;
        screen->winid= win->winid;
-       BLI_strncpy(win->screenname, screen->id.name+2, 21);
+       BLI_strncpy(win->screenname, screen->id.name+2, sizeof(win->screenname));
        
        wm->winactive= win;
        wm->file_saved= 1;
index aa99e8f..ca37f0d 100644 (file)
@@ -195,7 +195,7 @@ static void wm_window_match_do(bContext *C, ListBase *oldwmlist)
                                                else 
                                                        win->screen= ED_screen_duplicate(win, screen);
                                                
-                                               BLI_strncpy(win->screenname, win->screen->id.name+2, 21);
+                                               BLI_strncpy(win->screenname, win->screen->id.name+2, sizeof(win->screenname));
                                                win->screen->winid= win->winid;
                                        }
                                }
index bbbe49c..35a3fbc 100644 (file)
@@ -216,7 +216,7 @@ wmWindow *wm_window_copy(bContext *C, wmWindow *winorig)
        
        /* duplicate assigns to window */
        win->screen= ED_screen_duplicate(win, winorig->screen);
-       BLI_strncpy(win->screenname, win->screen->id.name+2, 21);
+       BLI_strncpy(win->screenname, win->screen->id.name+2, sizeof(win->screenname));
        win->screen->winid= win->winid;
 
        win->screen->do_refresh= 1;